How to generate 5 random numbers in mysql stored procedure

后端 未结 1 1205
清歌不尽
清歌不尽 2021-01-20 02:07

How can I generate 5 unique random numbers?

Now I have something like

declare v_counter integer;
declare v_random integer;
declare v_result varchar(1         


        
相关标签:
1条回答
  • 2021-01-20 02:36

    If your range is small, and you have an integers table, a naive approach might work:

    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;
    
    0 讨论(0)
提交回复
热议问题