让我们首先创建一个表-
mysql> create table DemoTable1339 -> ( -> Name varchar(30), -> Score int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1339 values('Chris',56); mysql> insert into DemoTable1339 values('Bob',46); mysql> insert into DemoTable1339 values('Adam',78); mysql> insert into DemoTable1339 values('John',90); mysql> insert into DemoTable1339 values('Carol',98);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1339;
这将产生以下输出-
+-------+-------+ | Name | Score | +-------+-------+ | Chris | 56 | | Bob | 46 | | Adam | 78 | | John | 90 | | Carol | 98 | +-------+-------+ 5 rows in set (0.00 sec)
以下是按NAME列对随机行进行排序的查询-
mysql> select * from (select *from DemoTable1339 order by rand() limit 4) tbl order by Name;
这将产生以下输出-
+-------+-------+ | Name | Score | +-------+-------+ | Adam | 78 | | Bob | 46 | | Carol | 98 | | Chris | 56 | +-------+-------+ 4 rows in set (0.05 sec)