I have 3 tables: schedule
and locations
, suites
.
Schedule's structure: schedule-id
, location-id
, schedule-completed-on-date
Locations' structure: location-id
, location-building-number
, location-street-name
Suites' structure: suite-id
, location-id
, suite-number
, suite-visited
Many suites may belong to same location. Schedule's location-id
is unique, so there may be only one unique location scheduled at any time, no duplicates.
I want to set schedule-completed-date-on
to a date, which I know how to do, if every single suite for a location of a schedule row have suite-visited
equal to 1
(0- no, 1- yes).
Is it possible to do with MySQL, and if so then how? Or do I have to do it programmatically using a server side language?
For example, if I have two locations - 1 Main St., and 2 Main St. - and 10 suites in total - 5 belong to first location, other 5 belong to second location. If MySQL detects that all 5 suite rows that relate to first location have suite-visited
equal to 1
, then immediately set schedule-completed-on
to current date, otherwise don't.
Data at hand and desired result:
LOCATIONS
location-id location-building-number location-street-name
1 1 Main St.
2 2 Main St.
SUITES
suite-id location-id suite-number suite-visited
1 1 100 1
2 1 200 0
3 1 300 0
4 1 400 0
5 1 500 1
6 2 1000 1
7 2 1100 1
8 2 1200 1
9 2 1300 1
10 2 1400 1
SCHEDULE
schedule-id location-id schedule-completed-on-date
1 1 NULL
2 2 7/3/2015 00:00:00
// because all suites for location 2 have suite-visited set to 1
// second row under SCHEDULE gets the date set
// because all suites belonging to
// location 2 have been visited