如何在MySQL中使用条件订购和选择查询?
以下是语法-
select * from yourTableName order by yourColumnName=0,yourColumnName;
让我们首先创建一个表-
mysql> create table DemoTable1348 -> ( -> Amount int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1348 values(100); mysql> insert into DemoTable1348 values(0); mysql> insert into DemoTable1348 values(90); mysql> insert into DemoTable1348 values(45); mysql> insert into DemoTable1348 values(0); mysql> insert into DemoTable1348 values(89);
使用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)