Generating a range of numbers in MySQL

前端 未结 11 1771
一个人的身影
一个人的身影 2020-11-22 15:07

How do I generate a range of consecutive numbers (one per line) from a MySQL query so that I can insert them into a table?

For example:

nr
1
2
3
4
5
         


        
相关标签:
11条回答
  • 2020-11-22 15:55

    This is based on a previous answer (https://stackoverflow.com/a/53125278/2009581), but is compatible with MySQL 5.7. It works for replicas and read-only users:

    SELECT x1.N + x10.N*10 + x100.N*100 + x1000.N*1000
      FROM (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) x1,
           (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) x10,
           (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) x100,
           (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) x1000
     WHERE x1.N + x10.N*10 + x100.N*100 + x1000.N*1000 <= @max;
    

    It generates integers in the range of [0, @max].

    0 讨论(0)
  • 2020-11-22 15:58
    DECLARE i INT DEFAULT 0;
    
    WHILE i < 6 DO
      /* insert into table... */
      SET i = i + 1;
    END WHILE;
    
    0 讨论(0)
  • 2020-11-22 16:00

    Here's a hardware engineer's version of Pittsburgh DBA's solution:

    SELECT
        (TWO_1.SeqValue + TWO_2.SeqValue + TWO_4.SeqValue + TWO_8.SeqValue + TWO_16.SeqValue) SeqValue
    FROM
        (SELECT 0 SeqValue UNION ALL SELECT 1 SeqValue) TWO_1
        CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 2 SeqValue) TWO_2
        CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 4 SeqValue) TWO_4
        CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 8 SeqValue) TWO_8
        CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 16 SeqValue) TWO_16;
    
    0 讨论(0)
  • 2020-11-22 16:02

    Very similar to the accepted response, but using the new WITH syntax for mysql >= 8.0 which makes a lot more legible and the intent is also clearer

    WITH DIGITS (N) AS (
      SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL
      SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL
      SELECT 8 UNION ALL SELECT 9)
    SELECT 
      UNITS.N + TENS.N*10 + HUNDREDS.N*100 + THOUSANDS.N*1000 
    FROM 
      DIGITS AS UNITS, DIGITS AS TENS, DIGITS AS HUNDREDS, DIGITS AS THOUSANDS;
    
    0 讨论(0)
  • 2020-11-22 16:04

    If you need the records in a table and you want to avoid concurrency issues, here's how to do it.

    First you create a table in which to store your records

    CREATE TABLE `incr` (
      `Id` int(11) NOT NULL auto_increment,
      PRIMARY KEY  (`Id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
    

    Secondly create a stored procedure like this:

    DELIMITER ;;
    CREATE PROCEDURE dowhile()
    BEGIN
      DECLARE v1 INT DEFAULT 5;
      WHILE v1 > 0 DO
        INSERT incr VALUES (NULL);
        SET v1 = v1 - 1;
      END WHILE;
    END;;
    DELIMITER ;
    

    Lastly call the SP:

    CALL dowhile();
    SELECT * FROM incr;
    

    Result

    Id
    1
    2
    3
    4
    5
    
    0 讨论(0)
提交回复
热议问题