简体   繁体   中英

How to Create Trigger After Insert with select data from Multiple table with Join in MYSql

I am trying to Create Trigger in MySQL with Select Column data from Join multiple table. But Trigger is not allow me to DECLARE temp variable.

I would like to join 4 table on the bases of newly inserted record in one table and select the data from different table and insert r update in another table (DashboardStatus)

I am getting error [ SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET _BedId = (SELECT bd.BedId
FROM LifetouchHeartRate lthr JOIN Device' at line 4 */ ]


CREATE TABLE `dashboardstatus` (
    `LTHR` INT(11) NULL DEFAULT NULL,
    `BedId` INT(11) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;



CREATE TRIGGER triggerDashboard AFTER INSERT ON   LifetouchHeartRate    for each row 
BEGIN   
DECLARE _BedId  INT 
SET _BedId    = (SELECT bd.BedId As _BedId    
FROM  LifetouchHeartRate lthr 
JOIN DeviceSession ds ON ds.DeviceSessionID = lthr.ByDevSessionId
JOIN PatientSession ps ON ps.PatientSessionId = ds.ByPatientSessionId
JOIN PatientDetails pd ON pd.PatientDetailsId = ps.ByPatientId
JOIN BedDetails bd ON bd.BedDetailsId = pd.ByBedId
WHERE lthr.LifeTouchHeartRateID =  new.LifeTouchHeartRateID Limit 1 );

IF _BedId > 0
    BEGIN
           INSERT OR REPLACE INTO DashboardStatus (LTHR, BedId)  VALUES ( new.LifeTouchHeartRateID, _BedId)
    END
 END

Remember ;

...
CREATE TRIGGER triggerDashboard AFTER INSERT ON   LifetouchHeartRate    for each row 
BEGIN   
-- DECLARE _BedId  INT
DECLARE _BedId INT;
...

UPDATE

DELIMITER //

CREATE TRIGGER triggerDashboard AFTER INSERT ON LifetouchHeartRate
FOR EACH ROW
BEGIN
  -- DECLARE _BedId INT
  DECLARE _BedId INT;
  SET _BedId = (SELECT bd.BedId As _BedId
                FROM  LifetouchHeartRate lthr 
                  JOIN DeviceSession ds ON ds.DeviceSessionID = lthr.ByDevSessionId
                  JOIN PatientSession ps ON ps.PatientSessionId = ds.ByPatientSessionId
                  JOIN PatientDetails pd ON pd.PatientDetailsId = ps.ByPatientId
                  JOIN BedDetails bd ON bd.BedDetailsId = pd.ByBedId
                WHERE lthr.LifeTouchHeartRateID = new.LifeTouchHeartRateID Limit 1 );

  /*IF _BedId > 0
        BEGIN
              INSERT OR REPLACE INTO DashboardStatus (LTHR, BedId)  VALUES ( new.LifeTouchHeartRateID, _BedId)
        END
  END*/

  IF _BedId > 0 THEN
    -- BEGIN
      INSERT INTO DashboardStatus (LTHR, BedId)  VALUES ( new.LifeTouchHeartRateID, _BedId);
    -- END
  END IF;
END//

DELIMITER ;

SQL Fiddle demo

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