【发布时间】:2025-11-26 16:40:01
【问题描述】:
我正面临一种情况,需要帮助。我有两张桌子:
用户:
-
user_id、fname、lname、email。
user_timesheet:
-
time_id、user_id、month、state、date。
用户将向user_time 表添加时间,状态为 = 'no',并在月底提交更改状态 = 'yes' 的时间,假设月份为 JUNE
我想写一个查询,它将把所有没有添加时间的用户和添加了时间但还没有提交六月的用户。
这是我的查询。
SELECT user_timesheet.time_id, user_timesheet.user_id,
user_timesheet.month, user_timesheet.`state`,
`user`.user_id, `user`.fname, `user`.lname,
`user`.email
FROM user LEFT JOIN
user_timesheet ON user.user_id=user_timesheet.user_id
WHERE (
user_timesheet.state = 'no' OR
user_timesheet.state IS NULL)
AND (
user_timesheet.month = 'june' OR
user_timesheet.month IS NULL)
GROUP BY user.user_id
结果会将所有在 6 月添加时间但已提交的用户以及自加入后从未添加时间的用户带到系统。但是,它不会带来上个月添加时间或提交时间但根本没有添加 6 月份时间的用户。
【问题讨论】: