日期:2014-05-16  浏览次数:20707 次

mysql有没有一个算法生成一个特定id
随机生成id

------解决方案--------------------
没什么好办法,一种常见方法是,利用RAND随机函数生成一个,然后检查一下是否已经存在,如是存在则继续随机,直至找到一个不存在的。
------解决方案--------------------
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)