我如何让sql根据条件以不同方式订购相同的列?如下例所示,如果n/s
为n
,则为order by distfromc desc
,如果不是order by distfromc asc
,则显示如下表所示。
name n/s distfromc
BH n 2
FV n 1
C c 0
RS s 1
MTN s 2
答案 0 :(得分:1)
SELECT tablename.*
FROM tablename
ORDER BY
`n/s` = 'n' DESC,
CASE WHEN `n/s` = 'n' THEN distfromc END DESC,
CASE WHEN `n/s` != 'n' THEN distfromc END ASC
请参阅小提琴here。