-2

用户可以看到他们在本周上传的内容。我有以下代码:

它是否正确?

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

我为 (userid, uploaddate) 创建了索引。

4

1 回答 1

0

要使您的原始查询工作day需要在您正在查询的数据库中(或设置为变量),并且它需要在 MySQL 日期“0000-00-00”或日期时间“0000-00-00 00”中: 00:00' 格式。

WEEKDAY()为输入的日期返回 0-6。

$first; //is earliest offset in the range
$last; //is the latest offset in the range

SELECT * FROM images WHERE userid = '$userid'
AND uploadeddate > CURDATE() - INTERVAL $first DAY
AND uploadeddate < CURDATE() - INTERVAL $last DAY
ORDER BY uploadeddate DESC

所以像这样的查询将返回上周的结果。

SELECT * FROM images WHERE userid = '$userid' AND
uploadeddate > CURDATE() - INTERVAL 14 DAY
AND uploadeddate < CURDATE() - INTERVAL 7 DAY
ORDER BY uploadeddate DESC
于 2013-04-18T00:53:05.080 回答