码迷,mamicode.com
首页 > 数据库 > 详细

Mysql中随机函数笔记

时间:2016-06-17 11:05:36      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

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中随机函数笔记

标签:

原文地址:http://www.cnblogs.com/zhuzhiq/p/5593270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!