我想:如果a不为null,则按a升序排列,否则按b降序排列,所以我写:
select a, b from table order by ifnull(a asc, b desc);
但是 MySQL 抱怨语法错误
有什么建议么?
我想:如果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
如果您只想order bywhen ais NULLvalue 并将订单保留为 when ais NON NULL,那么您可以执行以下操作:
select a, b from test order by a asc,
case when a IS NULL THEN b ELSE 0 END desc
这是SQLFiddlea ,您可以在此小提琴中看到 sql 在具有值时保持原样,仅在具有NON NULL值时才对它们进行排序。aNULL
你真的不需要这里的 IF 语句。如果列“a”为空,那么它不会影响基于列“b”对结果进行排序的方式。
SELECT a, b
FROM table
ORDER BY a asc, b desc;