我希望用户可以在本周看到他们上传的图片。
这是对的吗?
"SELECT *
FROM images
WHERE userid = '$userid'
AND uploadeddate >= DATEPART(week, uploadeddate) = DATEPART(week, GETDATE())
ORDER BY uploadeddate DESC";
我收到错误。谢谢你的帮助。
答案 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())