我是mysql的新手,我想知道如何选择 从上周一到周五的数据?假如说 今天是星期一。这是我到目前为止尝试的但它返回了一个空结果:
SELECT * , SUM(time) AS total_project_hours
FROM task
WHERE DATE_SUB(NOW(), INTERVAL 1 WEEK ) BETWEEN 0 AND 4 AND deleted='n'
GROUP BY project_id
答案 0 :(得分:1)
使用DAYOFWEEK http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_dayofweek
SELECT * , SUM(time) AS total_project_hours FROM task WHERE DAYOFWEEK(date) >= 2 AND DAYOFWEEK(date) <= 6;
答案 1 :(得分:0)
试试这个:
SELECT t.project_id, SUM(t.time) AS total_project_hours
FROM task t
WHERE WEEK(t.date) = WEEK(CURRENT_DATE()) AND DAYOFWEEK(t.date) IN (2,3,4,5,6) AND
deleted='n'
GROUP BY project_id