0

I want to run a MYSQL query to get data for the previous week. The datatype for the date column is DATETIME. Could anyone suggest?

4
  • 1
    Hmm, I think your question might suffer from posting/editing issues. Please clarify or read the FAQ. Commented Feb 11, 2013 at 11:02
  • Previous week? or last seven days? Commented Feb 11, 2013 at 11:11
  • stackoverflow.com/questions/12493995/get-data-from-last-week , And typecast your datetime to date stackoverflow.com/questions/4740612/… if it was what you were searching for, then my opinion is do some more google :) Commented Feb 11, 2013 at 11:13
  • For the last seven days... Commented Feb 11, 2013 at 11:59

5 Answers 5

11
SELECT * 
  FROM calendar 
 WHERE dt BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE();
Sign up to request clarification or add additional context in comments.

Comments

2

Here is an another version:

SELECT * FROM table WHERE 
YEARWEEK(`date`, 1) = YEARWEEK( CURDATE() - INTERVAL 1 WEEK, 1)

Comments

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

Comments

0

Here is the solution I find most reliable for getting data between the previus monday to the current monday. (That is what most people mean when the say past week, but not all, and that reflect in mysql).

SELECT 
   * 
FROM 
   table 
WHERE 
   date BETWEEN 
      (CURDATE() - INTERVAL 1 DAY) + INTERVAL -1 WEEK - INTERVAL WEEKDAY((CURDATE() - INTERVAL 1 DAY)) DAY 
      and 
      (CURDATE() - INTERVAL 1 DAY) + INTERVAL 0 WEEK - INTERVAL WEEKDAY((CURDATE() - INTERVAL 1 DAY)) DAY

It's also easy to change it for another week intervall

Comments

-1

Make variable for current datetime - 1 week and make this query:

SELECT * FROM table WHERE date > $datatime

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.