visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Databases :: MySql :: SQL: ORBER BY - difference in 4.0.12 and 3.22.32


By: eszet U.S.A.  Date: 19/05/2003 00:00:00  English  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 English  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 English  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 English  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 English  Type : Comment
and just ignore the extra 'a' columns :D
By: eszet Date: 20/05/2003 23:12:00 English  Type : Comment
Great, that's it!
Thanks.

Do register to be able to answer

EContact
browser fav
page generated in 1211.591960 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page