如何生成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
任何的想法?
如果范围很小,并且有一个整数表,则可以使用一种幼稚的方法:
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;