If I have:
ID | Title
1 | Shirt
2 | CD
3 | Cap
4 | Mp3
5 | Badge
If I want to sort by this order: 4, 2, 5, 3,1. Is there a way to do an sql query where you explicitly specify this? Something like:
select * from TABLE order by ID(4,2,5,3,1) ??
Actually, you were surprisingly close. It's a simple as:
select * from TABLE order by field(ID,4,2,5,3,1)
You can use a CASE in the ORDER BY as a rudimentary lookup table:
select *
from your_table
order by
case id
when 4 then 1
when 2 then 2
when 5 then 3
when 3 then 4
when 1 then 5
end
select * from TABLE order by ID=1,ID=3,ID=5,ID=2,ID=4;
你跑了这个,让我知道你的怀疑。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.