本文介绍了使用MySql,我可以对一列进行排序,但最后有0位吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按升序的int列排序,但我想让0排在最后.无论如何,在MySql中可以做到这一点吗?
I want to sort by an column of ints ascending, but I want 0 to come last. Is there anyway to do this in MySql?
推荐答案
您可能需要尝试以下操作:
You may want to try the following:
SELECT * FROM your_table ORDER BY your_field = 0, your_field;
测试用例:
CREATE TABLE list (a int);
INSERT INTO list VALUES (0);
INSERT INTO list VALUES (0);
INSERT INTO list VALUES (0);
INSERT INTO list VALUES (1);
INSERT INTO list VALUES (2);
INSERT INTO list VALUES (3);
INSERT INTO list VALUES (4);
INSERT INTO list VALUES (5);
结果:
SELECT * FROM list ORDER BY a = 0, a;
+------+
| a |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 0 |
| 0 |
| 0 |
+------+
8 rows in set (0.00 sec)
这篇关于使用MySql,我可以对一列进行排序,但最后有0位吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!