让我们首先创建一个表-
mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentMarks int -> );
使用insert命令在表中插入一些记录。我们还插入了重复记录-
mysql> insert into DemoTable(StudentMarks) values(98); mysql> insert into DemoTable(StudentMarks) values(98); mysql> insert into DemoTable(StudentMarks) values(78); mysql> insert into DemoTable(StudentMarks) values(78); mysql> insert into DemoTable(StudentMarks) values(45); mysql> insert into DemoTable(StudentMarks) values(56);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+--------------+ | StudentId | StudentMarks | +-----------+--------------+ | 1 | 98 | | 2 | 98 | | 3 | 78 | | 4 | 78 | | 5 | 45 | | 6 | 56 | +-----------+--------------+ 6 rows in set (0.00 sec)
以下是通过rand()
将它们分组来排序的查询-
mysql> select *from DemoTable order by rand(StudentMarks*100*RAND());
这将产生以下输出。在这里,标记98记录保持分组-
+-----------+--------------+ | StudentId | StudentMarks | +-----------+--------------+ | 3 | 78 | | 6 | 56 | | 5 | 45 | | 4 | 78 | | 1 | 98 | | 2 | 98 | +-----------+--------------+ 6 rows in set (0.00 sec)