0

I am loosing it a bit and cant think of this sql correctly

I have

tables: userid , counter , date and

I need to check how many files has user downloaded in last 24h , starting sql

SELECT SUM(counter) FROM download_log WHERE userid = 258 AND date = DATE(NOW())

but it is returning null , and I downloaded min 40 files ,
date format in date table is

2012-05-05 01:59:55

I also tried , date = NOW()

same thing what am i missing ?

thank you!

Benn
  • 4,840
  • 8
  • 65
  • 106

3 Answers3

1

Try something like this:

SELECT SUM(counter) FROM download_log WHERE userid = 258 AND date = CURDATE()

That should work.

Mohamed Nuur
  • 5,536
  • 6
  • 39
  • 55
1

I'd guess you are storing date and time in column named date , so it should work :

SELECT SUM(counter) FROM download_log WHERE userid = 258 AND 
DATE(`date`)= DATE(NOW())
a1ex07
  • 36,826
  • 12
  • 90
  • 103
  • this works perfect but I am wondering , this should be 24h right? – Benn Jul 26 '12 at 23:40
  • That doesn't actually return #files downloaded in last 24 hours ; it returns #files downloaded today (it may or may not be what you want). If you need exactly 24 hours from now, it should be `WHERE date > NOW() - INTERVAL 24 HOUR AND date < NOW()`. Also, "date" should be in backticks. – a1ex07 Jul 26 '12 at 23:44
1

Wouldn't it be:

SELECT SUM(counter) FROM download_log where userid = 258 and date >= DATE_SUB(NOW(), INTERVAL 24 HOUR);

You have to get all rows within the last 24 hours.

sceaj
  • 1,573
  • 3
  • 12
  • 24