| | |
Order By Group By Distinct Problem
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Sep 2007
Posts: 16
Reputation:
Solved Threads: 0
hi folks ,
I have a Table names A with these fields (Id,Entry_Date,State)
I want to fetch the 5 recent records with Unique States And Order them Desc
so this is what I did
I tried many ways to do this , first of All I tried to work with Distinct but I figured out that Distinct is applied to a Row not a field
so
it gives me the same result.
so I tried to solve the problem by group by
but As you know the group by has to be before order and its giving me some other results
so I am wondering how can I do this !!!
Please help me on this
I have a Table names A with these fields (Id,Entry_Date,State)
I want to fetch the 5 recent records with Unique States And Order them Desc
so this is what I did
sql Syntax (Toggle Plain Text)
SELECT * FROM A ORDER BY Entry_Date Desc LIMIT 5
so
sql Syntax (Toggle Plain Text)
SELECT DISTINCT State,Id FROM A ORDER BY Entry_Date Desc LIMIT 5
it gives me the same result.
so I tried to solve the problem by group by
sql Syntax (Toggle Plain Text)
SELECT DISTINCT State,Id FROM A GROUP BY State ORDER BY Entry_Date Desc LIMIT 5
but As you know the group by has to be before order and its giving me some other results
so I am wondering how can I do this !!!
Please help me on this
Last edited by peter_budo; Feb 21st, 2009 at 4:35 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
•
•
•
•
I tried many ways to do this , first of All I tried to work with Distinct but I figured out that Distinct is applied to a Row not a field
mysql Syntax (Toggle Plain Text)
SELECT DISTINCT state FROM A ORDER BY entry_date desc LIMIT 5
mysql Syntax (Toggle Plain Text)
SELECT DISTINCT State,Id FROM A GROUP BY State ORDER BY Entry_Date desc LIMIT 5
for example,
state --- id
state1 --> 1
state1 --> 2
state2 --> 3
state3 --> 4
The above query will return state1 --> 1 as well as state1-->2, since they make unique combination !
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Aug 2009
Posts: 1
Reputation:
Solved Threads: 0
I had this exact problem before, and I ran into again just yesterday. I finally came up with a simple solution, almost seems too simple. You need to use a subquery as a column of the select query. In that subquery is where you will do the ordering by date. When you do it all in a single query with GROUP BY ... ORDER BY ... the GROUP BY happens first, THEN the ORDER BY. You want to order first, so go with the subquery. I explain this in full detail in a blog post here:
<URL SNIPPED>
<URL SNIPPED>
Last edited by peter_budo; Aug 13th, 2009 at 12:43 pm. Reason: Keep It Spam Free - Do not spam, advertise, plug your website, or engage in any other type of self promotion.
![]() |
Similar Threads
- Making user choose values only from the combobox (VB.NET)
- Select Statement problem (ASP)
- Count/Sum distinct problem (MS SQL)
- print problem (PHP)
- Combobox filling from Access Database (VB.NET)
- Cold Fusion Loop problem (ColdFusion)
Other Threads in the MySQL Forum
- Previous Thread: how to set up a database to calculate marks
- Next Thread: How to make database searchable
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm database design developer development distinct drupal dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop legal license licensing linux maintenance mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign reorderingcolumns saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization






