使用 join in mysql 更新无效
update is not working using join in mysql
我在 SQL 加入和更新中遇到问题。尝试了“>=”/“=”/“<”的不同组合,但从未获得预期的结果。
Table: 'punchdata'
Id empID pType pDate pTime pInOut DeleteRec
32382 EMP-217 Attendance 2021-11-26 08:50:45 In 0
33540 EMP-217 Attendance 2021-11-26 12:34:21 Out 0
33171 EMP-217 Attendance 2021-11-26 17:13:49 Out 0
33170 EMP-217 Attendance 2021-11-26 17:09:49 Out 0
33168 EMP-217 Attendance 2021-11-26 17:11:18 In 0
33036 EMP-217 Attendance 2021-11-26 12:34:32 Out 0
SQL Statement:
update punchdata w1
join ( select min(id) as SIMPLEID, `empID`, `pDate`, `pTime`
from punchdata where pType = 'Attendance' AND pDate = '2021-11-26'
group by `empID`,`pDate`,`pTime` ) w2
on w1.id > w2.SIMPLEID
and w1.`empID` = 'EMP-217'
and w1.`pDate` = w2.`pDate`
and w1.`pTime` <= w2.`pTime`
and w1.`pInOut` = 'Out'
set w1.`DeleteRec` = '1'
Result:
Id empID pType pDate pTime pInOut DeleteRec
32382 EMP-217 Attendance 2021-11-26 08:50:45 In 0
33540 EMP-217 Attendance 2021-11-26 12:34:21 Out 1
33171 EMP-217 Attendance 2021-11-26 17:13:49 Out 0
33170 EMP-217 Attendance 2021-11-26 17:09:49 Out 1
33168 EMP-217 Attendance 2021-11-26 17:11:18 In 0
33036 EMP-217 Attendance 2021-11-26 12:34:32 Out 0
Intended Output:
Id empID pType pDate pTime pInOut DeleteRec
32382 EMP-217 Attendance 2021-11-26 08:50:45 In 0
33540 EMP-217 Attendance 2021-11-26 12:34:21 Out 1
33171 EMP-217 Attendance 2021-11-26 17:13:49 Out 0
33170 EMP-217 Attendance 2021-11-26 17:09:49 Out 1
33168 EMP-217 Attendance 2021-11-26 17:11:18 In 0
33036 EMP-217 Attendance 2021-11-26 ***12:34:32 Out 1***
有人可以帮忙吗?
您为什么决定应更新行 id=33036
?
参见 fiddle。具有 w1.id=33036
的 None 行的所有 5 个条件都等于 TRUE(参见 criteria_1
和 criteria_4
,至少其中一个为 FALSE)。
我在 SQL 加入和更新中遇到问题。尝试了“>=”/“=”/“<”的不同组合,但从未获得预期的结果。
Table: 'punchdata'
Id empID pType pDate pTime pInOut DeleteRec
32382 EMP-217 Attendance 2021-11-26 08:50:45 In 0
33540 EMP-217 Attendance 2021-11-26 12:34:21 Out 0
33171 EMP-217 Attendance 2021-11-26 17:13:49 Out 0
33170 EMP-217 Attendance 2021-11-26 17:09:49 Out 0
33168 EMP-217 Attendance 2021-11-26 17:11:18 In 0
33036 EMP-217 Attendance 2021-11-26 12:34:32 Out 0
SQL Statement:
update punchdata w1
join ( select min(id) as SIMPLEID, `empID`, `pDate`, `pTime`
from punchdata where pType = 'Attendance' AND pDate = '2021-11-26'
group by `empID`,`pDate`,`pTime` ) w2
on w1.id > w2.SIMPLEID
and w1.`empID` = 'EMP-217'
and w1.`pDate` = w2.`pDate`
and w1.`pTime` <= w2.`pTime`
and w1.`pInOut` = 'Out'
set w1.`DeleteRec` = '1'
Result:
Id empID pType pDate pTime pInOut DeleteRec
32382 EMP-217 Attendance 2021-11-26 08:50:45 In 0
33540 EMP-217 Attendance 2021-11-26 12:34:21 Out 1
33171 EMP-217 Attendance 2021-11-26 17:13:49 Out 0
33170 EMP-217 Attendance 2021-11-26 17:09:49 Out 1
33168 EMP-217 Attendance 2021-11-26 17:11:18 In 0
33036 EMP-217 Attendance 2021-11-26 12:34:32 Out 0
Intended Output:
Id empID pType pDate pTime pInOut DeleteRec
32382 EMP-217 Attendance 2021-11-26 08:50:45 In 0
33540 EMP-217 Attendance 2021-11-26 12:34:21 Out 1
33171 EMP-217 Attendance 2021-11-26 17:13:49 Out 0
33170 EMP-217 Attendance 2021-11-26 17:09:49 Out 1
33168 EMP-217 Attendance 2021-11-26 17:11:18 In 0
33036 EMP-217 Attendance 2021-11-26 ***12:34:32 Out 1***
有人可以帮忙吗?
您为什么决定应更新行 id=33036
?
参见 fiddle。具有 w1.id=33036
的 None 行的所有 5 个条件都等于 TRUE(参见 criteria_1
和 criteria_4
,至少其中一个为 FALSE)。