按降序排序然后升序同一列

时间:2014-04-09 10:15:17

标签: mysql sql-order-by

我如何让sql根据条件以不同方式订购相同的列?如下例所示,如果n/sn,则为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

1 个答案:

答案 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