How to set SQL to find records from last sunday to this sunday (1 week)

为君一笑 提交于 2019-12-01 05:54:01

To get the latest preceding sunday midnight, this should do it. Replace both instances of NOW() with your datetime to check another date.

SELECT DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())-1 DAY) latest_sun

To get the sunday one week earlier, instead use DAYOFWEEK(NOW())+6 DAY.

EDIT: That'd make your query;

SELECT COUNT(author)
FROM `posts` 
WHERE author='FooBar'
  AND `date` >= DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())+6 DAY)
  AND `date` <  DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())-1 DAY)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!