0

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

这个对吗?

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

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

4

2 回答 2

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)使其快速工作。

于 2013-04-17T23:28:18.447 回答
0

以下查询将返回本周的记录。在SELECT.

如果您使用的是 SQL Server:

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

如果您使用的是 MySQL:

SELECT ... WHERE WEEK(uploadeddate) = WEEK(CURDATE())
于 2013-04-17T23:25:32.830 回答