从本周的周一18:00选择值

时间:2018-06-25 20:01:11

标签: php mysql date time

我正试图仅在本周的星期一以及从18:00到19:00从MySQL获取值。我该怎么做?

结果应为:1

m_z_analytics

|id|m_type|date_added         |
|1 |test1 |2018-06-25 18:02:09|
|2 |test2 |2018-06-26 19:44:24|


SELECT COUNT(id) AS id FROM m_z_analytics WHERE
Week(date_added) = Week(CURRENT_TIMESTAMP) AND
date_added >= (NOW() - INTERVAL 7 DAY) AND
date_added < (NOW() - INTERVAL 6 HOUR)

1 个答案:

答案 0 :(得分:0)

使用WEEKDAY()DAYNAME()函数测试星期几,使用HOUR()函数测试星期几。

SELECT COUNT(*)
FROM m_z_analytics
WHERE date_added BETWEEN NOW() - INTERVAL 1 WEEK AND NOW() - INTERVAL 6 HOUR
AND HOUR(date_added) = 18
AND DAYNAME(date_added) = 'Monday'

您不需要WEEK()函数,因为时间间隔会缩小到当前星期。