Generate an integer sequence in MySQL

前端 未结 16 2981
南旧
南旧 2020-11-22 06:47

I need to do a join with a table/result-set/whatever that has the integers n to m inclusive. Is there a trivial way to get that without just buildi

16条回答
  •  深忆病人
    2020-11-22 07:24

    How big is m?

    You could do something like:

    create table two select null foo union all select null;
    create temporary table seq ( foo int primary key auto_increment ) auto_increment=9 select a.foo from two a, two b, two c, two d;
    select * from seq where foo <= 23;
    

    where the auto_increment is set to n and the where clause compares to m and the number of times the two table is repeated is at least ceil(log(m-n+1)/log(2)).

    (The non-temporary two table could be omitted by replacing two with (select null foo union all select null) in the create temporary table seq.)

提交回复
热议问题