SQL select current week / last week

I am looking for a way to select rows based on:

  • Current Week (SS)
  • Previous Week (SS)

The problem that I encountered is to choose exactly Sunday-Sunday .

I am currently using:

SELECT SUM(time) 
FROM `time` 
WHERE `projectid` = '$pid' && created > DATE_SUB(NOW(), INTERVAL 1 WEEK)

Any help would be great, thanks!

+4
source share
1 answer

MySQL has a function WEEKthat you can use:

SELECT SUM(time) 
FROM `time` 
WHERE
    `projectid` = '$pid'
    AND created > NOW() - INTERVAL 2 WEEK
    AND WEEK(created) IN (WEEK(NOW()), WEEK(NOW() - INTERVAL 1 WEEK))

Notes

(created > NOW() - INTERVAL 2 WEEK) , , , . , , . , .

"WEEK(NOW() - INTERVAL 1 WEEK)" - . WEEK(NOW()) - 1

+1

Source: https://habr.com/ru/post/1535251/


All Articles