标签:mysql生成随机数
生成15位以下随机数的函数:
DELIMITER $$ CREATE FUNCTION `getRand`(counts INTEGER) RETURNS varchar(20) CHARSET utf8 BEGIN DECLARE sTemp VARCHAR(20); DECLARE sTempCounts INTEGER; SET sTemp = ROUND(ROUND(RAND(),counts)*(POW(10,counts))); IF(CHAR_LENGTH(sTemp)<counts) THEN SET sTempCounts = counts - CHAR_LENGTH(sTemp); SET sTemp =CONCAT(sTemp,RIGHT(CONCAT(POW(10,sTempCounts),‘‘),sTempCounts)); END IF; 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)
标签:mysql生成随机数
原文地址:http://ldhqy.blog.51cto.com/2788106/1635945