PHP MySQL从星期一到星期五选择所有数据或从上周的工作日中选择所有数据

时间:2014-01-08 07:36:44

标签: mysql sql datetime select group-by

我是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

enter image description here

2 个答案:

答案 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