在 MySQL 中对非零整数重新排序?


要对非零整数重新排序,请使用以下语法:-

select *from yourTableName order by yourColumnName=0 ,yourColumnName;

让我们首先创建一个表:-

mysql> create table reorderIntegerExcept0
   -> (
   -> value int
   -> );
Query OK, 0 rows affected (0.70 sec)

以下是使用 insert 命令向表中插入记录的查询:-

mysql> insert into reorderIntegerExcept0 values(90);
Query OK, 1 row affected (0.17 sec)

mysql> insert into reorderIntegerExcept0 values(10);
Query OK, 1 row affected (0.21 sec)

mysql> insert into reorderIntegerExcept0 values(0);
Query OK, 1 row affected (0.18 sec)

mysql> insert into reorderIntegerExcept0 values(40);
Query OK, 1 row affected (0.18 sec)

mysql> insert into reorderIntegerExcept0 values(0);
Query OK, 1 row affected (0.09 sec)

mysql> insert into reorderIntegerExcept0 values(50);
Query OK, 1 row affected (0.17 sec)

mysql> insert into reorderIntegerExcept0 values(80);
Query OK, 1 row affected (0.13 sec)

mysql> insert into reorderIntegerExcept0 values(0);
Query OK, 1 row affected (0.10 sec)

mysql> insert into reorderIntegerExcept0 values(300);
Query OK, 1 row affected (0.20 sec)

以下是使用 select 语句显示表中所有记录的查询:-

mysql> select * from reorderIntegerExcept0;

这将产生以下输出:-

+-------+
| value |
+-------+
| 90    |
| 10    |
| 0     |
| 40    |
| 0     |
| 50    |
| 80    |
| 0     |
| 300   |
+-------+
9 rows in set (0.00 sec)

以下是重新排序非零整数的查询:-

mysql> select * from reorderIntegerExcept0 order by value =0 ,value;

这将产生以下输出:-

+-------+
| value |
+-------+
| 10    |
| 40    |
| 50    |
| 80    |
| 90    |
| 300   |
| 0     |
| 0     |
| 0     |
+-------+
9 rows in set (0.00 sec)

如果你希望非 0 整数降序排列,请使用以下查询:-

mysql> select * from reorderIntegerExcept0 order by value =0,value DESC;

这将产生以下输出:-

+-------+
| value |
+-------+
| 300   |
| 90    |
| 80    |
| 50    |
| 40    |
| 10    |
| 0     |
| 0     |
| 0     |
+-------+
9 rows in set (0.00 sec)

更新于:30-Jul-2019

65 次浏览

开启您的 职业生涯

完成课程获得认证

开始
广告