要随机播放元素,您需要使用ORDER BY RAND()
。让我们首先创建一个表-
mysql> create table DemoTable1557 -> ( -> SubjectId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> SubjectName varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1557(SubjectName) values('MySQL'); mysql> insert into DemoTable1557(SubjectName) values('MongoDB'); mysql> insert into DemoTable1557(SubjectName) values('Java'); mysql> insert into DemoTable1557(SubjectName) values('C');
使用选择显示表中的所有记录-
mysql> select * from DemoTable1557;
这将产生以下输出-
+-----------+-------------+ | SubjectId | SubjectName | +-----------+-------------+ | 1 | MySQL | | 2 | MongoDB | | 3 | Java | | 4 | C | +-----------+-------------+ 4 rows in set (0.00 sec)
以下是使用MySQL随机整理记录的查询-
mysql> select * from DemoTable1557 order by rand();
这将产生以下输出-
+-----------+-------------+ | SubjectId | SubjectName | +-----------+-------------+ | 1 | MySQL | | 3 | Java | | 2 | MongoDB | | 4 | C | +-----------+-------------+ 4 rows in set (0.00 sec)