Thursday, December 7, 2023

Create Sequence Using SQL in MYSQL

Generate a range of consecutive/Sequence numbers (1 to 100)  using a MySQL query, see below. Replace 100 number as per your choice, so it will generate sequence of numbers 1 to number you will going to replace

select t0.rn+t1.rn+t2.rn+t3.rn  as Sequence
from
    (select 0 rn union select 1    rn union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
    (select 0 rn union select 10   rn union select 20 union select 30 union select 40 union select 50 union select 60 union select 70 union select 80 union select 90) t1,
    (select 0 rn union select 100  rn union select 200 union select 300 union select 400 union select 500 union select 600 union select 700 union select 800 union select 900) t2,
    (select 0 rn union select 1000 rn union select 2000 union select 3000 union select 4000 union select 5000 union select 6000 union select 7000 union select 8000 union select 9000) t3
where
    t3.rn<=100
    and t2.rn<=100
    and t1.rn<=100
    and t0.rn+t1.rn+t2.rn+t3.rn<=100
    and t0.rn+t1.rn+t2.rn+t3.rn<>0
    order by 1

No comments:

Post a Comment

web stats