Generate an integer sequence in MySQL

前端 未结 16 3108
南旧
南旧 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:21

    Counter from 1 to 1000:

    • no need to create a table
    • time to execute ~ 0.0014 sec
    • can be converted into a view

    :

    select tt.row from
    (
    SELECT cast( concat(t.0,t2.0,t3.0,t4.0) + 1 As UNSIGNED) as 'row' FROM 
    (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t,
    (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2, 
    (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3
    ) tt
    order by tt.row
    

    Credits: answer, comment by Seth McCauley below the answer.

提交回复
热议问题