如何生成5个唯一的随机数?
现在我有类似
的东西declare v_counter integer;
declare v_random integer;
declare v_result varchar(10);
select FLOOR(1+(rand()*50)) into v_result;
set v_counter=0;
while v_counter < 4 then
select FLOOR(1+(rand()*50)) into v_random;
set v_result = concat(v_result,'|',v_random;
v_counter = v_counter + 1;
end while;
结果可能如下所示:
12|22|3|46|3
数字必须是唯一的并且排序如下:
1|2|3|4|5
有什么想法吗?
答案 0 :(得分:2)
如果你的范围很小,并且你有一个integers table,一个天真的方法可能会有效:
SELECT GROUP_CONCAT(i SEPARATOR '|')
FROM ( SELECT i
FROM ( SELECT i
FROM integers
WHERE i BETWEEN 1 AND 50
ORDER BY RAND()
LIMIT 5) sort_these_five
ORDER BY i) concat_these_five;