我有两张表tbl_Attendance
和tbl_leave
。这些表的值如下所示。
tbl_leave :
id | empcode | Marked | datetime_From | datetime_To
1 | emp1 | 2 | 19-10-2018 | 22-10-2018
1 | emp2 | 2 | 20-10-2018 | 20-10-2018
tbl_Attendance :
id | empcode | Marked | datetime_
1 | emp1 | 1 | 20-10-2018
1 | emp2 | 1 | 20-10-2018
1 | emp3 | 1 | 20-10-2018
1 | emp4 | 1 | 20-10-2018
1 | emp5 | 1 | 20-10-2018
现在我想将tbl_Attendance
列Marked
更改为2,如果表tbl_leave
中存在
tbl_Attendance
id | empcode | Marked | datetime_
1 | emp1 | 2 | 20-10-2018
1 | emp2 | 2 | 20-10-2018
1 | emp3 | 1 | 20-10-2018
1 | emp4 | 1 | 20-10-2018
1 | emp5 | 1 | 20-10-2018
请有人帮助我
答案 0 :(得分:1)
此更新查询应解决您的问题,请尝试使用。
update tbl_Attendance set Marked=2 where empcode in (select empcode from tbl_leave)
如果您想根据tbl_leave中的日期更新tbl_Attendance中的Marked,那么您可以尝试此查询:
update tbl_Attendance set Marked=2 where empcode in
(SELECT t.empcode from
(SELECT TL.empcode,TA.datetime_,TL.datetime_From,TL.datetime_To from
tbl_leave TL
INNER JOIN tbl_attendance TA ON TL.empCode=TA.empcode) t
where t.datetime_ BETWEEN t.datetime_From and t.datetime_To)
如果tbl_Attendance中的dateTime_介于tbl_Leave中的dateTime_From和dateTime_To之间,则上述查询将更新标记为2.