0

I want all data from last week. I used

SELECT id FROM tbl
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY 

but its not working when my week starts with "Monday".

What should I do?

4
  • The problem is you're using current date, not "monday" or whatever as you start point. so on thursday it would run between 6 days ago and yesterday.. Commented Sep 19, 2012 at 11:44
  • I want that if I start from whatever date it should always display last week starting from monday to sunday. Commented Sep 19, 2012 at 11:47
  • Exacrtly you havent taken into account that today is not Monday. Commented Sep 19, 2012 at 11:48
  • @BugFinder This sql return data with starting from sunday to monday. but I want monday to sunday. if I use +5 it work for only if current date is sunday, but when I change date its not work. Commented Sep 19, 2012 at 11:54

3 Answers 3

1
SELECT id FROM tbl WHERE date  >= CURDATE() - INTERVAL (WEEKDAY(CURDATE())+7) DAY  AND date   <  CURDATE()  - INTERVAL (WEEKDAY(CURDATE())) DAY 

I try this and it work for me.

Sign up to request clarification or add additional context in comments.

Comments

0

Try

SELECT id FROM tbl
WHERE YEARweek(date) = YEARweek(curdate())

Comments

0

Then change like this

$lastweek = unix_to_human(time("Y-m-d H:i:s") - (7 * 24 * 60 * 60), TRUE, 'us');
SELECT id FROM tbl
WHERE date >= curdate() - $lastweek
AND date < curdate() - $lastweek 

2 Comments

if I use +5 it work for only if current date is sunday, but when I change date its not work.
my project in cakephp here unix_to_human returns an error : Fatal error: Call to undefined method AdminStatisticController::unix_to_human()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.