-2

User can see what they have uploaded in this week. I have below code in line:

Is this correct?

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

I have create index for (userid, uploadeddate) .

1 Answers1

0

For your original query to work day needs to be in the database you're querying (or set as a variable) and it needs to be in a MySQL date '0000-00-00' or date time '0000-00-00 00:00:00' formats.

WEEKDAY() returns 0-6 for the date entered.

$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

So a query like this would return results for last week.

SELECT * FROM images WHERE userid = '$userid' AND
uploadeddate > CURDATE() - INTERVAL 14 DAY
AND uploadeddate < CURDATE() - INTERVAL 7 DAY
ORDER BY uploadeddate DESC
AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35