如何按条件来排序并选择 MySQL 中的查询?
以下是语法 −
select * from yourTableName order by yourColumnName=0,yourColumnName;
我们首先创建一个表 −
mysql> create table DemoTable1348 -> ( -> Amount int -> ); Query OK, 0 rows affected (0.80 sec)
使用 insert 命令在该表中插入一些记录 −
mysql> insert into DemoTable1348 values(100); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1348 values(0); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1348 values(90); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1348 values(45); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1348 values(0); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1348 values(89); Query OK, 1 row affected (0.12 sec)
使用 select 语句从该表中显示所有记录 −
mysql> select * from DemoTable1348;
这会生成以下输出 −
+--------+ | Amount | +--------+ | 100 | | 0 | | 90 | | 45 | | 0 | | 89 | +--------+ 6 rows in set (0.00 sec)
以下是对带有条件的查询进行排序和选择的查询 −
mysql> select * from DemoTable1348 order by Amount=0,Amount;
这会生成以下输出 −
+--------+ | Amount | +--------+ | 45 | | 89 | | 90 | | 100 | | 0 | | 0 | +--------+ 6 rows in set (0.00 sec)
广告