问题
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