Generate an integer sequence in MySQL

前端 未结 16 2980
南旧
南旧 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

    You could try something like this:

    SELECT @rn:=@rn+1 as n
    FROM (select @rn:=2)t, `order` rows_1, `order` rows_2 --, rows_n as needed...
    LIMIT 4
    

    Where order is just en example of some table with a reasonably large set of rows.

    Edit: The original answer was wrong, and any credit should go to David Poor who provided a working example of the same concept

提交回复
热议问题