views:

55

answers:

2

I have the following query:

SELECT id
FROM auctions
WHERE end_dt > TIME_TO_SEC(TIMEDIFF(end_dt, now())) > '0'
GROUP BY auctions.id
ORDER BY end_dt ASC
LIMIT 15

This is really slow. But basically, we're trying to select the most recent 15 records that are about to "end" by checking if the amount of seconds left is greater than zero.

I think I over complicated it, and figured I could do something like this:

SELECT id
FROM auctions
WHERE end_dt > now()
GROUP BY auctions.id
ORDER BY end_dt ASC
LIMIT 15

However, this returns different results.

Does anyone have a better solution?

Thank you!

+2  A: 

Try

WHERE `end_dt` BETWEEN DATE_SUB(NOW() , INTERVAL 15 MINUTE)
                           AND NOW()

Source: http://stackoverflow.com/questions/3480947/mysql-select-rows-where-timestamp-column-between-now-and-10-minutes-ago/3480977#3480977

krio
If I knew you were on, I would have waited for you to put it up =D
krio
Single quotes will cause the encapsulated text to be treated as string/varchar - I changed the single quotes to backticks, but backticks are only necessary if referencing a column/table name that is also a [MySQL reserved word](http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html)
OMG Ponies
This is also giving me different results.
Dave
A: 

Ok it ended up being correct by doing end_dt > now() - so many records were being inserted that it changed in between even the seconds that it took to try again..

Dave