我有一个表,其中的单列数字为1-100。想要生成每个数字N重复N次的输出(因此5应该出现5次):

1
2
2
3
3
3
4
4
4
4
and so on

最佳答案

对于mysql之前的8.0版本,

create table _set(n int);

insert into _set(n) values (1), (2), (3), (4), (5), (6); -- this goes on

select s2.* from _set as s
  join _set as s2 on s.n <= s2.n;


临时表不能两次打开documented issue

对于mysql版本8.0,请尝试此操作,

with _set as (select 1 as n
            union all
            select n+1 as n from _set
            where n < 100
            )
select s2.* from _set s
    join _set s2 on s.n <= s2.n

关于mysql - 使用SQL生成模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55373758/

10-10 21:40