让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int, -> Name varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(201,'Chris Brown'); mysql> insert into DemoTable values(110,'John Doe'); mysql> insert into DemoTable values(101,'Adam Smith'); mysql> insert into DemoTable values(345,'Carol Taylor'); mysql> insert into DemoTable values(135,'John Smith');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------+--------------+ | Id | Name | +------+--------------+ | 201 | Chris Brown | | 110 | John Doe | | 101 | Adam Smith | | 345 | Carol Taylor | | 135 | John Smith | +------+--------------+ 5 rows in set (0.00 sec)
这是对ORDER By字段的查询。Id 101将保留在顶部,其余记录将以相同顺序显示-
mysql> select *from DemoTable -> order by (Id=101) desc;
这将产生以下输出-
+------+--------------+ | Id | Name | +------+--------------+ | 101 | Adam Smith | | 201 | Chris Brown | | 110 | John Doe | | 345 | Carol Taylor | | 135 | John Smith | +------+--------------+ 5 rows in set (0.00 sec)