从MySql获取上周记录

时间:2012-09-26 08:27:11

标签: mysql sql phpmyadmin

我有一个包含大量记录的MySql表。但是我想要获取当前和时间的单独记录。上个礼拜。

查询当前周记录完美无缺

SELECT * FROM tbl_track WHERE WEEK(`date_created`) = WEEK(CURDATE())

查询上周,但它不起作用

SELECT * FROM tbl_track WHERE WEEK(`date_created`) = WEEK(CURDATE()) INTERVAL 7 DAY

2 个答案:

答案 0 :(得分:1)

SELECT * FROM tbl_track 
WHERE WEEK(`date_created`) = WEEK(DATE_SUB(CURDATE(), INTERVAL 7 DAY))

OR:

SELECT * FROM tbl_track 
WHERE WEEK(`date_created`) = WEEK(CURDATE() - INTERVAL 7 DAY)

答案 1 :(得分:-1)

看起来不像索引可以使用,但这确实是一个副作用。你希望你的curdate是 - 7天

SELECT * FROM tbl_track WHERE WEEK(date_created)= WEEK(ADDDATE(CURDATE(),INTERVAL -7 DAY));