我有一个包含大量记录的MySql表。但是我想要获取当前和时间的单独记录。上个礼拜。
查询当前周记录完美无缺
SELECT * FROM tbl_track WHERE WEEK(`date_created`) = WEEK(CURDATE())
查询上周,但它不起作用
SELECT * FROM tbl_track WHERE WEEK(`date_created`) = WEEK(CURDATE()) INTERVAL 7 DAY
答案 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));