SELECT the 10 largest value from a database field
Hi all,
Let's say I have a table with a DateTime field, How can i select the 10 of the latest date value from that?
(I want to get 10 latest date)
What i know is using MAX to get the latest date, but how to get several date instead of only one latest date?
Thank you.
Re: SELECT the 10 largest value from a database field
You should use "group by" to achieve your goal; something similar to this:
Code:
SELECT TOP 10 MAX(dateTimeField)
FROM yourTable
GROUP BY dateTimeField
ORDER BY dateTimeField DESC
Re: SELECT the 10 largest value from a database field
Maybe I'm confused...But, why not just use LIMIT?
Code:
SELECT dateTimeField FROM table ORDER BY dateTimeField DESC LIMIT 0, 10
Re: SELECT the 10 largest value from a database field
Quote:
Originally Posted by
PeejAvery
Maybe I'm confused...But, why not just use LIMIT?
Code:
SELECT dateTimeField FROM table ORDER BY dateTimeField DESC LIMIT 0, 10
Limit is MySQL specific - but you are right, there is no need to over complicate.
Code:
SELECT TOP 10 dateTimeField FROM table ORDER BY dateTimeField DESC
is enough - no need for MAX or GROUP BY or anything :D
I think I got confused by the mention of MAX and automatically resorted to GROUP :o.
Re: SELECT the 10 largest value from a database field
Quote:
Originally Posted by
Alsvha
Limit is MySQL specific
Ahhh, I did not know that.
Re: SELECT the 10 largest value from a database field
thanks, i have done it with TOP. :P