发布网友 发布时间:2022-04-09 04:10
共5个回答
懂视网 时间:2022-04-09 08:32
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)
Mysql中随机函数笔记
标签:
热心网友 时间:2022-04-09 05:40
select round(round(rand(),4)*10000);热心网友 时间:2022-04-09 06:58
我写了一个SQL2005的热心网友 时间:2022-04-09 08:32
这是php生成随机数热心网友 时间:2022-04-09 10:24
select round(round(rand(),4)*10000);
The rand function requires 2 to 3 arguments.
select round(round(rand(),4)*10000, 4);
保证生成四位整数,即1000~9999:
select cast(floor(rand()*10000) as int)%9000+1000;
释义:生成0~8999的随机数+1000,即可;