2014-01-24 56 views

回答

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 

sqlFiddle

或者,你可以寫一個函數爲你喜歡的下面,你可以指定你想要的字符串的長度,並把它檢查,以排除您在指定的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 ; 

sqlFiddle

相關問題