我需要一个条件ORDER BY语句。用外行人的话来说是这样的:
IF index_order IS NULL ORDER BY id ASC ELSE ORDER BY index_order ASC.因此,基本上,它将通过ID对具有空index_order值的所有行进行排序,然后通过index_order对其余的行进行排序。
这里的任何帮助都将不胜感激。
发布于 2014-05-02 22:48:02
只需使用:
ORDER BY index_order, id因为NULL排序总是低于任何其他值。因此,所有index_order为NULL的行都将首先分组。然后,他们的秩序将由id解决。
关于你的评论:
我试过了。
mysql> create table foo (id int auto_increment primary key, index_order int);
mysql> insert into foo values (1, 2), (2, 1), (3, null), (4, null);
mysql> select * from foo order by index_order, id;
+----+-------------+
| id | index_order |
+----+-------------+
| 3 | NULL |
| 4 | NULL |
| 2 | 1 |
| 1 | 2 |
+----+-------------+它首先命令index_order为null,并使用id排序升序解析绑定。然后通过非空index_order升序进行排序。
我不知道从你的描述中我是否理解你希望它能有不同的排序。
https://stackoverflow.com/questions/23438052
复制相似问题