本周选择MYSQL

时间:2013-04-17 23:22:44

标签: mysql

我希望用户可以在本周看到他们上传的图片。

这是对的吗?

"SELECT * 
FROM images 
WHERE userid = '$userid' 
  AND uploadeddate >= DATEPART(week, uploadeddate) = DATEPART(week, GETDATE()) 
ORDER BY uploadeddate DESC";

我收到错误。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

SELECT  *
FROM    images
WHERE   userid = $userid
        AND uploadeddate >= CURDATE() - INTERVAL WEEKDAY(day) DAY
        AND uploadeddate < CURDATE() - INTERVAL WEEKDAY(day) DAY + INTERVAL 7 DAY

(userid, uploadeddate)上创建一个索引,以便快速工作。

答案 1 :(得分:0)

以下查询将返回当前周的记录。最佳做法是在SELECT中指定列列表。

如果您使用的是SQL Server:

SELECT ... WHERE DATEPART(week, uploadeddate) = DATEPART(week, GETDATE())

如果您使用的是MySQL:

SELECT ... WHERE WEEK(uploadeddate) = WEEK(CURDATE())