随机生成id

解决方案 »

  1.   

    没什么好办法,一种常见方法是,利用RAND随机函数生成一个,然后检查一下是否已经存在,如是存在则继续随机,直至找到一个不存在的。
      

  2.   

    mysql> select cast(rand()*100 as SIGNED );
    +-----------------------------+
    | cast(rand()*100 as SIGNED ) |
    +-----------------------------+
    |                          44 |
    +-----------------------------+
    1 row in set (0.01 sec)mysql> select cast(rand()*100 as SIGNED );
    +-----------------------------+
    | cast(rand()*100 as SIGNED ) |
    +-----------------------------+
    |                          27 |
    +-----------------------------+
    1 row in set (0.00 sec)mysql> select cast(rand()*100 as SIGNED );
    +-----------------------------+
    | cast(rand()*100 as SIGNED ) |
    +-----------------------------+
    |                           1 |
    +-----------------------------+
    1 row in set (0.00 sec)