Sql order byexpression中的条件asc或desc

Sql order byexpression中的条件asc或desc,sql,Sql,我想:如果a不为null,则按a升序排序,否则按b降序排序,所以我写: select a, b from table order by ifnull(a asc, b desc); 但是MySQL抱怨语法错误 有什么建议吗?为什么不这样跳过ifnull: select a, b from test order by a asc, b desc 这是你的电话号码 如果仅当a为NULL值时才希望order by,并在a为非NULL时保留订单,则可以执行以下操作: select a, b fr

我想:如果a不为null,则按a升序排序,否则按b降序排序,所以我写:

  select a, b from table order by ifnull(a asc, b desc);
但是MySQL抱怨语法错误


有什么建议吗?

为什么不这样跳过
ifnull

select a, b from test order by a asc, b desc
这是你的电话号码

如果仅当
a
NULL
值时才希望
order by
,并在
a
非NULL
时保留订单,则可以执行以下操作:

select a, b from test order by a asc, 
case when a IS NULL THEN b ELSE 0 END desc

下面是一个例子,您可以在这个小提琴中看到,当
a
具有
非NULL
值时,sql保持原样的顺序,它只在
a
具有
NULL
值时才对它们进行排序。

这里确实不需要IF语句。如果列“a”为空,则它不会影响基于列“b”的结果排序方式


很抱歉,这毫无意义。假设您的a/b对为1/2、3/1、空/2。那么NULL/2应该在哪里呢?到目前为止,您的顺序是a=1,a=3(升序),然后您想向下插入b=2?你当然是指别的。例如,按照斯奈德的建议,先按asc排序,然后按b desc排序。请澄清你的要求。
SELECT a, b 
FROM table 
ORDER BY a asc, b desc;