2013-11-03 62 views
0

我創建這個函數應該返回一個隨機字符串:MySQL的函數返回什麼

create function createRandomString() returns Text 
     return concat( 
     char(round(rand()*25)), 
     char(round(rand()*25)), 
     char(round(rand()*25)), 
     char(round(rand()*25)), 
     char(round(rand()*25)), 
     char(round(rand()*25)), 
     char(round(rand()*25)), 
     char(round(rand()*25)) 
    );  

出於某種原因,這個函數返回什麼...我不知道爲什麼..

當我在選擇內使用完全相同的concat() - 它的作品。

回答

1

問題是,您正在返回介於0和25之間的數字值,這些數字不是要放入字符串的普通字符。添加65〜他們在「A」開始:

create function createRandomString() returns Text 
     return 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)) 
    ); 

你可能想在ASCII編碼以及讀了,讓你更好地瞭解你在做什麼。

+0

會做,謝謝! –