我无法做到这一点,我仍在使用连接,所以这可能是错误的。我有3个数据表和1个“摘要”表,其中包含我提交的值id。
我想总结来自2个不同表的值ID列,并将它们与当前日期和用户相关联。确实如此,但仅限于昨天?我需要它将查询更改为当天。我试着在那里获得CURDATE(),但我显然没有做得对。
SELECT U.user_name, SUM(T.cut_timetotal) AS sum_time, SUM(C.cuts_total) AS sum_cuts, DATE(S.submit_date ) AS date
FROM summary S
JOIN cut_info C ON S.cuts_id = C.cuts_id
JOIN users U ON U.user_id = S.user_id
JOIN timeT ON T.time_id = S.time_id
GROUP BY U.user_name
答案 0 :(得分:0)
这应该提供您所寻求的答案:
SELECT U.user_name, SUM(T.cut_timetotal) AS sum_time, SUM(C.cuts_total) AS sum_cuts, DATE(S.submit_date ) AS date
FROM summary S
JOIN cut_info C ON S.cuts_id = C.cuts_id
JOIN users U ON U.user_id = S.user_id
JOIN timeT ON T.time_id = S.time_id
WHERE S.submit_date BETWEEN CURDATE() AND CURDATE() + INTERVAL 1 DAY
GROUP BY U.user_name
使用
S.submit_date BETWEEN CURDATE() AND CURDATE() + INTERVAL 1 DAY
优于
DATE(S.submit_date) = CURDATE()
因为前者将使用索引(如果找到),后者则不会。