mysql rand函数

Returns a random floating-point valuevin the range0<=v<1.0. If a constant integer argumentNis specified, it is used as the seed value, which produces a repeatable sequence of column values.

若未指定参数,那么返回的值为0到1之间的浮点数;若指定了随机因子那么产生的值是固定的。

测试样例如下:

mysql> select rand(),rand(5);
+-------------------+-------------------+
| rand() | rand(5) |
+-------------------+-------------------+
| 0.406958082138522 | 0.406135974830143 |
+-------------------+-------------------+
1 row in set (0.00 sec)

mysql> select rand(),rand(5);
+------------------+-------------------+
| rand() | rand(5) |
+------------------+-------------------+
| 0.16665149123096 | 0.406135974830143 |
+------------------+-------------------+
1 row in set (0.00 sec)

mysql> select rand(),rand(5);
+-------------------+-------------------+
| rand() | rand(5) |
+-------------------+-------------------+
| 0.612383240472882 | 0.406135974830143 |
+-------------------+-------------------+
1 row in set (0.00 sec)
在官方手册中,还提到如下几点:
1、不要使用rand()在查询中,因为会造成多次扫描。
You cannot use a column withRAND()values in anORDER BYclause, becauseORDER BYwould evaluate the column multiple times.
2、在SBR复制中使用rand()不安全,使用rand()会引起一个警告,官方解释如下:
Beginning with MySQL 5.1.43, this function is flagged as unsafe for statement-based replication; use of this function causes a warning when using statement-based replication, and the statement is logged using the binary format whenbinlog_formatisMIXED. (Bug #49222)

posted on 2014-12-04 08:57  liufofu  阅读(249)  评论(0编辑  收藏  举报