本文介紹了先按特定 id 排序,然后按休息排序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
考慮一個包含兩列 RoleId 和 User Name 的示例表
Consider a Sample Table with two Column RoleId and User Name
Role | Name
1 AB
3 A
1 ABC
2 D
2 B
3 Abb
1 E
4 TE
如何使用 SQL 查詢獲得以下輸出.
How can i use SQL queries to get following Output.
Role | Name
3 A
3 Abb
1 AB
1 ABC
1 E
2 B
2 D
4 TE
我只想先按角色 ID 3 排序,然后再按剩余的角色 ID 排序.目前我正在使用 Union 來實現//
I just want to Order by Role Id 3 first then by remaining Roleid. Currently i am using Union to achieve so //
SELECT * FROM (SELECT * From @temp
Where roleid=3
UNION ALL
SELECT * From @temp
Where roleid != 3
) as X
推薦答案
您可以使用 case 進行更復雜的排序:
You can use case to make more complex ordering:
select *
from @temp
order by case when Role = 3 then 0 else 1 end, Role, Name
這篇關于先按特定 id 排序,然后按休息排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!