嗨,我有一个sql语句,从表中按降序返回行,我订购的列是requestStatus。 requestStatus可以是3个挂起,批准和拒绝的值。我想改变sql,如果可能的话,通过挂起然后批准,然后拒绝。我的sql语句如下。我正在使用asp.net razor视图引擎。谁能指出我正确的方向?
var dbCommand2 = "SELECT * FROM LeaveRequests WHERE email = @0 ORDER BY WHERE requestStatus DESC";
var rows1 = db.Query(dbCommand2, theUserName);
答案 0 :(得分:3)
在SELECT语句中使用CASE:
SELECT *, CASE requestStatus
WHEN 'pending' THEN 0
WHEN 'approved' THEN 1
WHEN 'rejected' THEN 2
END AS requestStatusOrder
FROM LeaveRequests
WHERE email = @0
ORDER BY requestStatusOrder