92

Lets say I have the table below.

I want to get all the friends, but I want the id 5 to be the first item in the list. I don't care about the order that I receive the rest of the items.

The desired query result will be:

friends
-------

id    name

5     nahum
1     moshe
2     haim
3     yusuf
4     gedalia
6     dana

How can I do this?

using Mysql 5.1.x.

Thanks!

tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149
ufk
  • 30,912
  • 70
  • 235
  • 386

7 Answers7

162
select id,name 
from friends 
order by id=5 desc

(given you don't care about order of the rest, otherwise, e.g. rest by id asc)

select id,name 
from friends 
order by id=5 desc, id asc
RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
  • 11
    @maxx777 This question is tagged MySQL, which is now owned by Oracle. Not "sql-server", as in Microsoft SQL Server. – RichardTheKiwi Aug 28 '14 at 21:54
  • 1
    I think most queries works on all DBMSs. And if someone says that this query is not working on a particular DBMS, usually he's asking for solution for that particular DBMS. – maxx777 Aug 29 '14 at 05:57
  • 9
    @maxx777 That's why we have tags and separate questions. It worked for the asker for his MySQL problem. If you want SQL Server 2005, the other answer has it, no need to comment on the obvious that this answer doesn't work on what it was never advertised to work on. – RichardTheKiwi Aug 31 '14 at 22:28
54

Try this:

select id,name 
from friends 
order by case when id=5 then -1 else id end

if you have more then one you can do:

select id,name 
from friends 
order by case when id in (5,15,25) then -1 else id end,id
Dumitrescu Bogdan
  • 7,127
  • 2
  • 23
  • 31
5

I can't access a MySQL now to test, so it might be reversed... but you can use the fact that Booleans also sort, and that you can have several sort fields.

SELECT ... ORDER BY id != 5, id

(you might have to write id = 5, I can't remember if TRUEs sort before or after FALSEs.)

EDIT: Oh, I just read that you don't care about the order of the rest, in which case I heartily recommend @Richard's answer.

Amadan
  • 191,408
  • 23
  • 240
  • 301
5

You can use field() in MySQL.

select id,name from friends order by field(id,5,id)

The 1st parameter in field() means the field you want to sort with, the rest is ordering.

So 5 will be sort first, and the rest from id (without 5). You can do like field(id,5,1,3,id) if you want 5,1,3 to be in front.

5 can be choose to sort at last by field(id,id,5). The 2nd id will exclude 5 from it also.

Jeffrey Neo
  • 3,693
  • 2
  • 26
  • 30
2

If you want to do the same for UNION query, for example if you have:

select id,name 
from friends 
UNION
select id,name 
from friends 
order by id=5 desc

... you would get an exception in PostgreSQL:

Only result column names can be used, not expressions or functions. HINT: Add the expression/function to every SELECT, or move the UNION into a from clause

To get around this, you would use the following:

select id,name, (id=5) AS is_five
from friends 
UNION
select id,name, (id=5) AS is_five
from friends 
order by is_five DESC, id DESC

The expression (id=5) would return 't' OR 'f', depending on whether your column value is equal or not to the expected value (5), so the order by would first order the 't' columns, then the rest.

Ucello
  • 226
  • 2
  • 16
2

You should use MySQL's ORDER BY FIELD clause to solve this. Although, the answer has been accepted on this, here's a better solution.

select 1 id, 'Zeta' order_col union all
select 2 id, 'Alpha' order_col union all
select 3 id, 'Gamma' order_col union all
select 4 id, 'Phi' order_col union all
select 5 id, 'Delta' order_col union all
select 6 id, 'Delta' order_col union all
select 7 id, 'Alpha' order_col union all
select 8 id, 'Gamma' order_col union all
select 9 id, 'Zeta' order_col union all
select 10 id, 'Phi' order_col 
order by field (order_col, 'Alpha', 'Gamma', 'Phi', 'Delta', 'Zeta'), id;

This is better than

  • id=something, order by id asc
  • order by case when something then 1 when something_else then 2 end desc
MontyPython
  • 2,906
  • 11
  • 37
  • 58
  • 1
    Not sure why this was downvoted. I'd like to know why order by field should be better but the answer is interesting.. – John Nov 02 '18 at 20:18
  • @John - This is better mostly because of ease in readability. Also, it is a solution that MySQL is providing us out of the box. It just seems to me that when you have to specify the order for a lot of values in a column, this makes more sense. – MontyPython Nov 06 '18 at 07:28
  • 1
    I think people simply did not understand that you provided an example with the "union all" and the solution to the question with the ORDER BY field. Thus the downvotes, it's just due to incompetence. – John Nov 06 '18 at 19:47
  • I check all my answers using keeping in mind @Strawberry's post - https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query. – MontyPython Nov 12 '18 at 07:16
-7

This is a little ugly because it has code duplication, but it does the trick:

select .... where id = 5 
union
select .... where not id = 5
Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
  • 3
    If rows remain ordered after a union, that's just an accident. And if you specify an `order by`, it will sort the entire union. – Andomar Mar 24 '11 at 10:59