Mysql 多列复杂排序 [英] Mysql complex sorting by multiple columns
本文介绍了Mysql 多列复杂排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下充满数据的 MySQL 数据库表:
I have following MySQL db table filled with data:
|id|parentId|position
|20|NULL |1
|21|NULL |2
|22|NULL |3
|23|21 |1
|24|21 |2
是否可以对其进行排序以获得最终结果:
Is it possible to sort it to get final result as:
|id|parentId|position
|20|NULL |1
|21|NULL |2
|23|21 |1
|24|21 |2
|22|NULL |3
您可能已经注意到,父级中的位置从 1 开始.
As you may notice, position inside parent starts from 1.
所以我希望它按三列排序:id、parentId 和 position.有什么想法吗?
So I want it sorted by three columns: id, parentId and position. Any ideas?
推荐答案
我想你想要什么?
ORDER BY IFNULL(parentId, id), parentId, position
这篇关于Mysql 多列复杂排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文