1,测试表结构:mysql> desc test_user;+---------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(50) | NO | | NULL | | | age | int(11) | YES | | NULL | | | address | varchar(50) | YES | | NULL | | 2,添加随机字段和索引:mysql> alter table test_user add random double;mysql> alter table test_user add index (random) 3,填充随机字段数据:mysql> update test_user set random = rand(); 4,随机返回一条数据: mysql> select id ,name from test_user where random > rand() order by random limit 1;
+----+------+
| id | name |
+----+------+
| 9 | com |
+----+------+
1 row in set (0.00 sec)
mysql> select id ,name from test_user where random > rand() order by random limi
t 1;
+----+------+
| id | name |
+----+------+
| 7 | www |
+----+------+
1 row in set (0.00 sec)
时间: 2024-09-29 19:48:19