-1
A
回答
0
感謝您的答覆。我建立一個查詢,對我來說工作正常..
SELECT CONCAT(
(SELECT CASE WHEN a.aa='0' THEN 'a'
WHEN a.aa='1' THEN 'b'
WHEN a.aa='l' THEN 'g'
WHEN a.aa='o' THEN 'q'
ELSE a.aa
END AS d
FROM (SELECT LCASE(CONV(FLOOR(RAND()*36),10,36)) AS aa) AS a),
(SELECT CASE WHEN a.aa='0' THEN 'a'
WHEN a.aa='1' THEN 'b'
WHEN a.aa='l' THEN 'g'
WHEN a.aa='o' THEN 'q'
ELSE a.aa
END AS d
FROM (SELECT LCASE(CONV(FLOOR(RAND()*36),10,36)) AS aa) AS a),
(SELECT CASE WHEN a.aa='0' THEN 'a'
WHEN a.aa='1' THEN 'b'
WHEN a.aa='l' THEN 'g'
WHEN a.aa='o' THEN 'q'
ELSE a.aa
END AS d
FROM (SELECT LCASE(CONV(FLOOR(RAND()*36),10,36)) AS aa) AS a),
(SELECT CASE WHEN a.aa='0' THEN 'a'
WHEN a.aa='1' THEN 'b'
WHEN a.aa='l' THEN 'g'
WHEN a.aa='o' THEN 'q'
ELSE a.aa
END AS d
FROM (SELECT LCASE(CONV(FLOOR(RAND()*36),10,36)) AS aa) AS a),
(SELECT CASE WHEN a.aa='0' THEN 'a'
WHEN a.aa='1' THEN 'b'
WHEN a.aa='l' THEN 'g'
WHEN a.aa='o' THEN 'q' ELSE a.aa
END AS d
FROM (SELECT LCASE(CONV(FLOOR(RAND()*36),10,36)) AS aa) AS a),
(SELECT CASE WHEN a.aa='0' THEN 'a'
WHEN a.aa='1' THEN 'b'
WHEN a.aa='l' THEN 'g'
WHEN a.aa='o' THEN 'q' ELSE a.aa
END AS d
FROM (SELECT LCASE(CONV(FLOOR(RAND()*36),10,36)) AS aa) AS a),
(SELECT CASE WHEN a.aa='0' THEN 'a'
WHEN a.aa='1' THEN 'b'
WHEN a.aa='l' THEN 'g'
WHEN a.aa='o' THEN 'q'
ELSE a.aa
END AS d
FROM (SELECT LCASE(CONV(FLOOR(RAND()*36),10,36)) AS aa) AS a)
)AS NAME
3
您可以使用RAND()函數和CHAR()函數來生成隨機字符以獲取ascii字符。下面的例子將給你20行字母數字(長度爲5個字符),它將包括你在底部使用HAVING
子句指定的字符。
CHAR(FLOOR(RAND()*10)+48)
會給你一個隨機數字從0至9
CHAR(FLOOR(RAND()*26)+65)
會給你一個隨機大寫字母
CHAR(FLOOR(RAND()*26)+97)
會給你一個隨機小寫字母
這些都是UNION-ED一起然後ORDER BY rand() LIMIT 1
將選擇一個隨機出現的一個字符使其對每個字符都是隨機的,然後將該代碼剪切並粘貼5次以獲得5個字符的隨機字符串。
SELECT CONCAT(
(SELECT CHAR(FLOOR(RAND()*10)+48) as chr UNION SELECT CHAR(FLOOR(RAND()*26)+65) UNION SELECT CHAR(FLOOR(RAND()*26)+97) ORDER BY rand() LIMIT 1),
(SELECT CHAR(FLOOR(RAND()*10)+48) as chr UNION SELECT CHAR(FLOOR(RAND()*26)+65) UNION SELECT CHAR(FLOOR(RAND()*26)+97) ORDER BY rand() LIMIT 1),
(SELECT CHAR(FLOOR(RAND()*10)+48) as chr UNION SELECT CHAR(FLOOR(RAND()*26)+65) UNION SELECT CHAR(FLOOR(RAND()*26)+97) ORDER BY rand() LIMIT 1),
(SELECT CHAR(FLOOR(RAND()*10)+48) as chr UNION SELECT CHAR(FLOOR(RAND()*26)+65) UNION SELECT CHAR(FLOOR(RAND()*26)+97) ORDER BY rand() LIMIT 1),
(SELECT CHAR(FLOOR(RAND()*10)+48) as chr UNION SELECT CHAR(FLOOR(RAND()*26)+65) UNION SELECT CHAR(FLOOR(RAND()*26)+97) ORDER BY rand() LIMIT 1)
) as randstr
FROM
(SELECT 1 FROM
(SELECT 0 as v UNION SELECT 1)T1,
(SELECT 0 as v UNION SELECT 2)T2,
(SELECT 0 as v UNION SELECT 4)T3,
(SELECT 0 as v UNION SELECT 8)T4,
(SELECT 0 as v UNION SELECT 16)T5,
(SELECT 0 as v UNION SELECT 32)T6
)T
HAVING randstr NOT LIKE '%1%'
AND randstr NOT LIKE '%0%'
AND randstr NOT LIKE '%o%'
AND randstr NOT LIKE '%l%'
LIMIT 20
或者,你可以寫一個函數爲你喜歡的下面,你可以指定你想要的字符串的長度,並把它檢查,以排除您在指定的4個字符,以生成一個隨機字符串你的問題
DELIMITER //
CREATE FUNCTION randString(in_length INT) RETURNS VARCHAR(50)
BEGIN
SET @len = 0;
SET @returnValue = "";
WHILE (@len < in_length) DO
SET @randChar = "0";
WHILE (@randChar IN ("0","1","o","l")) DO
SET @randChar = ( SELECT CHAR(FLOOR(RAND()*10)+48) as chr
UNION SELECT CHAR(FLOOR(RAND()*26)+65)
UNION SELECT CHAR(FLOOR(RAND()*26)+97)
ORDER BY rand() LIMIT 1);
END WHILE;
SET @returnValue = CONCAT(@returnValue,@randChar);
SET @len = @len + 1;
END WHILE;
RETURN @returnValue;
END//
DELIMITER ;
相關問題
- 1. 隨機生成器字母和整數
- 2. 如何生成隨機字母數字字符串?
- 3. 隨機數字/字母值
- 4. 生成隨機字母的函數
- 5. 生成隨機字母在Java中
- 6. 生成隨機字母和數字一起
- 7. 生成包含數字和字母的隨機密碼
- 8. 生成獨特的隨機字母數字字符串
- 9. sed的風格替換隨機字母,數字字母隨機數字
- 10. 生成隨機數字的字母/數字,但不能使用相同的字母/數字兩次
- 11. SugarCRM/MySQL如何自動生成一個隨機的字母數字號碼?
- 12. 如何在MySQL中生成唯一的字母數字內容?
- 13. 用於生成隨機字母數字值的SOAP UI方法
- 14. 生成短的,僞隨機可驗證字母數字代碼
- 15. 某些字母數字的句子
- 16. 隨機字母分佈限制
- 17. 如何seprate字母數字字符串轉換成數字和字母
- 18. 隨機字母數字引腳?
- 19. 用小寫字母和數字生成唯一的隨機字符串
- 20. RandomStringGenerator生成字母數字字符串
- 21. 生成的字母數字字符串
- 22. F#Canopy - 生成隨機字母和/或數字並在變量中使用
- 23. 如何從mysql中的AES_ENCRYPT()生成字母數字密碼?
- 24. 如何匹配字母,數字和字母數字值
- 25. 巨大的MySQL隨機字母數字字符串
- 26. 隨機函數隨機化字母
- 27. PHP和c#:如何爲Web內容生成字母數字ID?
- 28. 如何生成一個字符串使用字母和數字?
- 29. 從隨機ASCII字母和數字製作列表
- 30. 如何限制md5()函數PHP生成8個隨機字符和數字?
你應該指定長度限制也 –