当前位置:首页 > 编程技术 > 正文

mysql如何得到随机字符串

mysql如何得到随机字符串

```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

```

最新文章