Mysql中随机函数笔记
Posted JAVA菜菜鸟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql中随机函数笔记相关的知识,希望对你有一定的参考价值。
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中随机函数笔记的主要内容,如果未能解决你的问题,请参考以下文章