简体   繁体   中英

MYSQL update a row if all multiple rows that belong to the same foreign ID have same value

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

Trigger for auto update of SCHEDULE based on update of suite-visited table, name the trigger appropriately.

DELIMITER $$      
  CREATE TRIGGER trigger_name
  AFTER UPDATE
     ON suite-visited FOR EACH ROW      
  BEGIN      
      IF NOT EXISTS (select 1 from SUITES where suite-visited = 0 and location-id = new.location-id) THEN
          UPDATE SCHEDULE set schedule-completed-on-date = curdate() where location-id = new.location-id;
      END IF;
  END;
$$
DELIMITER ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM