Databases :: MySql :: SQL: ORBER BY - difference in 4.0.12 and 3.22.32 |
|||
| By: eszet |
Date: 19/05/2003 00:00:00 |
Points: 50 | Status: Answered Quality : Excellent |
|
Hi, I have a table with posts, some of them have a field "sticky" set to "true". On a server running MySQL 4.0.12 I can execute the following query: SELECT * FROM posts ORDER BY sticky='true' DESC, date DESC, id DESC In the results the "sticky-posts" will be displayed first ordered by date and id, after that, the other (non-sticky) posts will be displayed ordered by date and id. I have to implement the same on a server running 3.22.32. But on that server MySQL complains on the "sticky='true'" part. How could I accomplish the same in 3.22.32? (So I want the records with sticky set to true always displayed on top). Thanks. |
|||
| By: VGR | Date: 19/05/2003 00:16:00 | Type : Comment |
|
| SELECT * FROM posts ORDER BY if(sticky='true',1,0) DESC, date DESC, id DESC |
|||
| By: eszet | Date: 20/05/2003 18:11:00 | Type : Comment |
|
| VGR, I tried this but it doesn't seem to work: mysql> SELECT * FROM posts ORDER BY IF (sticky='true',1,0) DESC, date DESC, id DESC; ERROR 1064: You have an error in your SQL syntax near 'IF (sticky='true',1,0) DESC, date DESC, id DESC' at line 1 Any more ideas? Thanks, Stef. |
|||
| By: VGR | Date: 20/05/2003 21:54:00 | Type : Answer |
|
| sorry : SELECT *,IF (sticky='true',1,0) AS a FROM posts ORDER BY a DESC, date DESC, id DESC; |
|||
| By: VGR | Date: 20/05/2003 21:55:00 | Type : Comment |
|
| and just ignore the extra 'a' columns :D |
|||
| By: eszet | Date: 20/05/2003 23:12:00 | Type : Comment |
|
| Great, that's it! Thanks. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








