|
-
February 26th, 2009, 03:34 AM
#1
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.
-
February 26th, 2009, 06:43 AM
#2
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
-
February 26th, 2009, 09:24 AM
#3
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
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
February 26th, 2009, 11:13 AM
#4
Re: SELECT the 10 largest value from a database field
 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 
I think I got confused by the mention of MAX and automatically resorted to GROUP .
-
February 26th, 2009, 01:15 PM
#5
Re: SELECT the 10 largest value from a database field
 Originally Posted by Alsvha
Limit is MySQL specific
Ahhh, I did not know that.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
February 26th, 2009, 09:32 PM
#6
Re: SELECT the 10 largest value from a database field
thanks, i have done it with TOP. :P
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|
Bookmarks