| | |
Trouble using ORDER BY in a UNION statement
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
As the title suggests, I'm having trouble sorting the results of a union statement. I'm trying to sort the results by date descending and it's coming out ascending.
I'm selecting all the same rows from the same table. The reason I'm using a UNION statement is because I have 3 different WHERE conditions.
This really should work and I've gone over my code with a fine tooth comb. It's driving me crazy. Anyone have any suggestions?
mysql Syntax (Toggle Plain Text)
(SELECT a, b, c, d, e, f, DATE FROM table1 WHERE a=10 AND b=1) UNION (SELECT a, b, c, d, e, f, DATE FROM table1 WHERE a=11 AND b=1) ORDER BY DATE DESC;
This really should work and I've gone over my code with a fine tooth comb. It's driving me crazy. Anyone have any suggestions?
This user has a spatula. We don't know why, but we are afraid.
0
#2 Oct 8th, 2009
Try this
You need to encapsulate the whole union to order them after all the records are retrieved.
sql Syntax (Toggle Plain Text)
SELECT a,b,c,d,e,f FROM ( (SELECT a, b, c, d, e, f, DATE FROM table1 WHERE a=10 AND b=1) UNION (SELECT a, b, c, d, e, f, DATE FROM table1 WHERE a=11 AND b=1) ) ORDER BY DATE DESC
You need to encapsulate the whole union to order them after all the records are retrieved.
Last edited by cgyrob; Oct 8th, 2009 at 12:19 pm.
0
#3 Oct 8th, 2009
•
•
•
•
Try this
sql Syntax (Toggle Plain Text)
SELECT a,b,c,d,e,f FROM ( (SELECT a, b, c, d, e, f, DATE FROM table1 WHERE a=10 AND b=1) UNION (SELECT a, b, c, d, e, f, DATE FROM table1 WHERE a=11 AND b=1) ) ORDER BY DATE DESC
You need to encapsulate the whole union to order them after all the records are retrieved.
Thanks for the response. My original statement is actually correct. I was being blonde and didn't change a class variable on my display page which was causing the issues.
This user has a spatula. We don't know why, but we are afraid.
![]() |
Similar Threads
- ORDER BY (MySQL)
- "{" expected .. please help me (Java)
- Need help with mysql select statement and checkboxes (PHP)
- Sql + access - less than end date not working (MS Access and FileMaker Pro)
- SQL Union/ASP problem (MS SQL)
- Fourtune Teller Project, switch statement confusion (C++)
- Read string and use in switch statement (C++)
- trouble compiling (Java)
- Problems with switch statement (C++)
- change statement (JSP)
Other Threads in the MySQL Forum
- Previous Thread: Help with generating a unique number
- Next Thread: Help...! (Sub Query)
| Thread Tools | Search this Thread |
Tag cloud for MySQL
1 agplv3 alfresco amazon api artisticlicense aws breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement count court crm data database design developer development drupal ec2 email enter enterprise error eudora facebook form foss gartner gnu gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keywords kickfire laptop law legal license licensing linux maintenance managing matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource operand oracle pdf penelope php priceupdating query referencedesign reorderingcolumns resultset saas search sharepoint sourcecode spotify sql sugarcrm syntax techsupport transparency update virtualization





