使用 MySQL 生成一个唯一的随机 10 个字符的字符串?

mysqlmysqli database

为了生成一个 10 个字符的字符串,我们可以使用内置函数"rand()"和"char()"。以下是生成随机 10 个字符的字符串的查询。

mysql>  SELECT concat(
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97)
   - > )AS Random10CharacterString;

以下是显示随机 10 个字符串的输出。

+-------------------------+
| Random10CharacterString |
+-------------------------+
| duscikyspy              |
+-------------------------+
1 row in set (0.00 sec)

以下是生成大写随机字符的查询。

mysql> select concat(
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65)
   - > )AS Random10CharacterString;

以下是输出。

+-------------------------+
| Random10CharacterString |
+-------------------------+
| WMWWVOIXPF              |
+-------------------------+
1 row in set (0.00 sec)

上述输出以大写形式显示随机字符。


相关文章