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

这个对吗?

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

我出错了。 谢谢您的帮助。

===============>>#1 票数: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)以使其快速运行。

===============>>#2 票数:0

以下查询将返回当前一周的记录。 SELECT指定列列表也是最佳实践。

如果您使用的是SQL Server:

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

如果您使用的是MySQL:

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

  ask by user2292887 translate from so

未解决问题?本站智能推荐: