mysql文档中 是这么说的 :
  RAND() RAND(N) 
  返回一个随机浮点值 v ,范围在 0 到1 之间 (即, 其范围为 0 ≤ v ≤ 1.0)。若已指定一个整数参数 N ,则它被用作种子值,用来产生重复序列。  给一个固定的参数 rand()的值固定 如:
select rand(1),rand(1),rand(1)
union all
select rand(1),rand(1),rand(1);
输出没有问题但当我用这个时select rand(1) from staff;(staff是一个包含多条记录的表),
输出的每个结果都是不一样的 这是怎么回事呢?
每个结果按说都该是rand(1)算出来的 但怎么会不一样呢?求指导……

解决方案 »

  1.   

    每一行,RAND会重算一遍。 所以会产生不同的值。这个函数与其它函数不同。
      

  2.   

    但重算一遍不也是rand(1)吗 那应该也相同才是啊
      

  3.   

    我就是不明白
    mysql> SELECT i, RAND(3) FROM t;
    +------+------------------+
    | i | RAND(3) |
    +------+------------------+
    | 1 | 0.90576975597606 |
    | 2 | 0.37307905813035 |
    | 3 | 0.14808605345719 |
    +------+------------------+
    3 rows in set (0.01 sec)
    为什么rand(3)重复算后会出现不同的值
      

  4.   

    问你个问题,为什么
    SELECT i FROM table会有3个值
      

  5.   

    呃 那是因为table表中有三条记录
    2楼举例子时加的
    mysql> INSERT INTO t VALUES(1),(2),(3);
      

  6.   

    In the following example, note that the sequences of values produced by RAND(3) is the same both places where it occurs.mysql> CREATE TABLE t (i INT);
    Query OK, 0 rows affected (0.42 sec)mysql> INSERT INTO t VALUES(1),(2),(3);
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0mysql> SELECT i, RAND() FROM t;
    +------+------------------+
    | i    | RAND()           |
    +------+------------------+
    |    1 | 0.61914388706828 |
    |    2 | 0.93845168309142 |
    |    3 | 0.83482678498591 |
    +------+------------------+
    3 rows in set (0.00 sec)mysql> SELECT i, RAND(3) FROM t;
    +------+------------------+
    | i    | RAND(3)          |
    +------+------------------+
    |    1 | 0.90576975597606 |
    |    2 | 0.37307905813035 |
    |    3 | 0.14808605345719 |
    +------+------------------+
    3 rows in set (0.00 sec)mysql> SELECT i, RAND() FROM t;
    +------+------------------+
    | i    | RAND()           |
    +------+------------------+
    |    1 | 0.35877890638893 |
    |    2 | 0.28941420772058 |
    |    3 | 0.37073435016976 |
    +------+------------------+
    3 rows in set (0.00 sec)mysql> SELECT i, RAND(3) FROM t;
    +------+------------------+
    | i    | RAND(3)          |
    +------+------------------+
    |    1 | 0.90576975597606 |
    |    2 | 0.37307905813035 |
    |    3 | 0.14808605345719 |
    +------+------------------+
    3 rows in set (0.01 sec)
    With a constant initializer, the seed is initialized once when the statement is compiled, prior to execution. 
      

  7.   

    With a constant initializer, the seed is initialized once when the statement is compiled, prior to execution. 有种子的时候  , 语句在执行前已经全部初始化
      

  8.   

    rand(1)应该也是个数组,如果一条查询语句有多条记录,那么第一条到第N条记录会分别对应rand(1)数组中的值,,,这是我自己观察的,这么理解不知道对不对
    mysql> select rand(2) from courier_t where courier_id<2;
    +-------------------+
    | rand(2)           |
    +-------------------+
    | 0.655586646549019 |
    +-------------------+
    1 row in set (0.00 sec)mysql> select rand(2) from courier_t where courier_id<3;
    +-------------------+
    | rand(2)           |
    +-------------------+
    | 0.655586646549019 |
    | 0.122346619258026 |
    +-------------------+
    2 rows in set (0.00 sec)mysql> select rand(2) from courier_t where courier_id<4;
    +-------------------+
    | rand(2)           |
    +-------------------+
    | 0.655586646549019 |
    | 0.122346619258026 |
    |  0.64497318737672 |
    +-------------------+
    3 rows in set (0.00 sec)mysql> select rand(2) from courier_t where courier_id<5;
    +-------------------+
    | rand(2)           |
    +-------------------+
    | 0.655586646549019 |
    | 0.122346619258026 |
    |  0.64497318737672 |
    | 0.857826109843167 |
    +-------------------+
    4 rows in set (0.00 sec)mysql> select rand(2) from courier_t where courier_id<6;
    +-------------------+
    | rand(2)           |
    +-------------------+
    | 0.655586646549019 |
    | 0.122346619258026 |
    |  0.64497318737672 |
    | 0.857826109843167 |
    | 0.354211017819318 |
    +-------------------+
    5 rows in set (0.00 sec)
      

  9.   

    这个得看RAND函数的源代码来分析了。从一般理解, 计算机同样是无法产生真正的随机数的。所以说计算机函数产生的是一个“伪随机数” 产生这个随机数,你可以参考http://wenku.baidu.com/view/b5d892ee0975f46527d3e155.html以得到了解。当然也还有其它不同的随机数产生算法。但一般常用是这种。
      

  10.   

    可以参考下 这篇文章 http://www.hwei.org/archives/232
      

  11.   

    如果非要使 rand(1)在一段时间内是一个固定值,建议使用变量保存,使用游标 表 staff,并给staff表添加一个冗余字段进行标示!