生成15位以下随机数的函数:
DELIMITER $$CREATE FUNCTION `getRand`(counts INTEGER) RETURNS varchar(20) CHARSET utf8BEGIN DECLARE sTemp VARCHAR(20); DECLARE sTempCounts INTEGER; SET sTemp = ROUND(ROUND(RAND(),counts)*(POW(10,counts))); IF(CHAR_LENGTH(sTemp)counts) then SET sTemp = RIGHT(sTemp,counts); END IF; return sTemp;END$$DELIMITER ;
取数:
mysql> select getRand(4);+------------+| getRand(4) |+------------+| 9878 |+------------+1 row in set (0.00 sec)