2013-02-11 157 views
-2

我想運行一個MYSQL查詢來獲取前一週的數據。日期列的數據類型是DATETIME。有誰可以建議?MySQL查詢獲取上週的數據

+1

嗯,我認爲你的問題可能會受到影響從發佈/編輯問題。請澄清或閱讀常見問題解答。 – span 2013-02-11 11:02:06

+0

上週?或過去七天? – Strawberry 2013-02-11 11:11:52

+0

http://stackoverflow.com/questions/12493995/get-data-from-last-week,並typecast您的datetime到日期http://stackoverflow.com/questions/4740612/query-to-convert-from-datetime -to-date-mysql 如果這是你正在尋找的,那麼我的意見是做更多谷歌:) – arjuncc 2013-02-11 11:13:57

回答

10
SELECT * 
    FROM calendar 
WHERE dt BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE(); 
-1

使變量當前日期時間 - 1周,使這個查詢:

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

這裏是另一個版本:

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