使用 MySQL 生成一個 10 個字元的唯一隨機字串?
要生成一個 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)
上面的輸出顯示了大寫的隨機字元。
廣告