SQL ORDER BY (sequence) [duplicate]

醉酒当歌 提交于 2020-01-05 04:40:33

问题


I have a sql statement which I would want to ORDER BY a specific sequence.

SELECT * FROM UserDB ORDER BY [Role]

How can I make it such that the data brought to my GridView table is listed from Admin on the top, follow by User and Guests?


回答1:


So you want to order by Admin/User/Guest?

Try something like :

SELECT * 
FROM UserDB 
ORDER BY
  CASE Role WHEN 'Admin' THEN 0 
            WHEN 'User' THEN 1
            WHEN 'Guest' THEN 2
  END

Does that work for you?

Another option would be to have (or add) a column Sequence to your Role table so you could define the sequence in the table itself - and then just to an ORDER BY Role.Sequence.




回答2:


This is substantively identical to the question " sql ORDER BY multiple values in specific order? " and I strongly recommend you look at the solutions presented there.



来源:https://stackoverflow.com/questions/8471539/sql-order-by-sequence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!