如何在mysql的单个语句中插入两个不同的表

时间:2015-12-30 04:18:03

标签: mysql

这里插入一个表的查询在mysql事件中工作正常但是我需要用这个语句插入另一个表。

insert into tbl_attendance(TraineeID, attnDate, inTime, outTime, Status)
SELECT t1.TraineeID,
       t2.attnDate,
       MIN(t1.attnTime) AS inTime,
              (CASE WHEN MIN(t1.attnTime) < MAX(t1.attnTime) THEN MAX(t1.attnTime) else '' end) AS outTime,
       (CASE WHEN t4.Slot ='1' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '1')THEN 'P' 
             WHEN t4.Slot ='2' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '2')THEN 'P'
             WHEN t4.Slot ='3' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '3')THEN 'P'
             WHEN t4.Slot ='4' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '4')THEN 'P'
             WHEN t4.Slot ='1' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '1')THEN 'Early Out' 
             WHEN t4.Slot ='2' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '2')THEN 'Early Out'
             WHEN t4.Slot ='3' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '3')THEN 'Early Out'
             WHEN t4.Slot ='4' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '4')THEN 'Early Out'
             ELSE 'Late-In'
       END) AS Status

FROM tbl_attn_temp t1, tbl_assigned t3, tbl_instructor_info t4
JOIN (SELECT DISTINCT attnDate FROM tbl_attn_temp) t2
WHERE t1.attndate = t2.attnDate AND t1.TraineeID = t4.InstructorID
AND t1.attnDate>(SELECT max(attnDate) FROM tbl_attendance WHERE t1.TraineeID=TraineeID)
AND(CASE WHEN t4.Slot ='1' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '1') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '1') 
         WHEN t4.Slot ='2' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '2') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '2')
         WHEN t4.Slot ='3' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '3') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '3')
         WHEN t4.Slot ='4' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '4') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '4')
    END)
GROUP BY t1.TraineeID, t2.attnDate

1 个答案:

答案 0 :(得分:0)

mySql doesn't support multiple table INSERT query using only one query, I believe.
You will need to use separate INSERT query to insert in two different tables.

e.g. INSERT INTO tbl1 VALUES ();
     INSERT INTO tbl2 VALUES ();

You can alternatively use START TRANSACTION and COMMIT.

START TRANSACTION;
INSERT INTO tbl1 VALUES ();
INSERT INTO tbl2 VALUES ();
COMMIT;