MySQL - Ordering all data in alphabetical order but placing a particular item last
这个问题已经被问到MySql,不幸的是这个解决方案对SQL Server不起作用。这是我当前的查询和结果:
select ShipMethodID As UseMe, Name As ShowMe
from Purchasing.ShipMethod
union
Select 0 As UseMe, 'n/a' As ShowMe
order by ShowMe
结果:
5 CARGO TRANSPORT 5
0 n/a
4 OVERNIGHT J-FAST
3 OVERSEAS - DELUXE
1 XRQ - TRUCK GROUND
2 ZY - EXPRESS
我需要这样排序:
0 n/a
5 CARGO TRANSPORT 5
4 OVERNIGHT J-FAST
3 OVERSEAS - DELUXE
1 XRQ - TRUCK GROUND
2 ZY - EXPRESS
答案 0 :(得分:4)
select * from (
select ShipMethodID As UseMe, Name As ShowMe
from Purchasing.ShipMethod
union
Select 0 As UseMe, 'n/a' As ShowMe
) t
order by (case when UseMe = 0 then null else ShowMe end)
答案 1 :(得分:3)
首先按字段排序,先输入N / A,然后输入你真正要排序的字段 - 如果你真的想要n / a,那么order by DisplayOrder DESC, ShowMe
。
select ShipMethodID As UseMe, Name As ShowMe, 1 AS DisplayOrder
from Purchasing.ShipMethod
union
Select 0 As UseMe, 'n/a' As ShowMe, 0 AS DisplayOrder
order by DisplayOrder, ShowMe
答案 2 :(得分:2)
请尝试使用此查询..
select ShipMethodID As UseMe, Name As ShowMe, 1 As dummycolumn
from Purchasing.ShipMethod
union
Select 0 As UseMe, 'n/a' As ShowMe, 0 As dummycolumn
order by dummycolumn, ShowMe