MySQL查询排序值大于零的行?
让我们首先创建一个表。以下是查询-
mysql> create table gettingAndOrderingRowsDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Value int -> );
以下是使用insert命令在表中插入一些记录的查询-
mysql> insert into gettingAndOrderingRowsDemo(Value) values(10); mysql> insert into gettingAndOrderingRowsDemo(Value) values(13); mysql> insert into gettingAndOrderingRowsDemo(Value) values(0); mysql> insert into gettingAndOrderingRowsDemo(Value) values(20); mysql> insert into gettingAndOrderingRowsDemo(Value) values(30); mysql> insert into gettingAndOrderingRowsDemo(Value) values(60); mysql> insert into gettingAndOrderingRowsDemo(Value) values(0); mysql> insert into gettingAndOrderingRowsDemo(Value) values(45); mysql> insert into gettingAndOrderingRowsDemo(Value) values(0);
以下是使用select语句显示表中所有记录的查询-
mysql> select * from gettingAndOrderingRowsDemo;
这将产生以下输出-
+----+-------+ | Id | Value | +----+-------+ | 1 | 10 | | 2 | 13 | | 3 | 0 | | 4 | 20 | | 5 | 30 | | 6 | 60 | | 7 | 0 | | 8 | 45 | | 9 | 0 | +----+-------+ 9 rows in set (0.00 sec)
以下是要查询的大于零的值-
mysql> select * from gettingAndOrderingRowsDemo -> order by Value=0,Value;
这将产生以下输出-
+----+-------+ | Id | Value | +----+-------+ | 1 | 10 | | 2 | 13 | | 4 | 20 | | 5 | 30 | | 8 | 45 | | 6 | 60 | | 3 | 0 | | 7 | 0 | | 9 | 0 | +----+-------+ 9 rows in set (0.00 sec)