mysql如何得到随机字符串
- 编程技术
- 2025-01-26 03:56:52
- 1
```sqlDELIMITER $$CREATE FUNCTION `RandomString`(length INT RETURNS VARCHAR(255 BEG...
```sql
DELIMITER $$
CREATE FUNCTION `RandomString`(length INT) RETURNS VARCHAR(255)
BEGIN
DECLARE random_str VARCHAR(255) DEFAULT '';
DECLARE character_list VARCHAR(255) DEFAULT 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
DECLARE i INT DEFAULT 0;
DECLARE rand_index INT;
WHILE i < length DO
SET rand_index = FLOOR(RAND() LENGTH(character_list) + 1);
SET random_str = CONCAT(random_str, SUBSTRING(character_list, rand_index, 1));
SET i = i + 1;
END WHILE;
RETURN random_str;
END$$
DELIMITER ;
```
要使用这个函数,你可以像这样调用它:
```sql
```
本文由夕逆IT于2025-01-26发表在夕逆IT,如有疑问,请联系我们。
本文链接:http://www.xinin56.com/bian/343912.html
本文链接:http://www.xinin56.com/bian/343912.html