根据给定的计数多次插入相同的记录
insert same record multiple times based on the given count
TYPE COUNT
--- --
ABC 3
EDC 4
FDC 2
我想在 table 中使用单个 SQL 插入三次 ABC,四次 EDC,两次 FDC,可以吗?
查询的输出应使用以下条目插入到以下 table 中。
TYPE
----
ABC
ABC
ABC
EDC
EDC
EDC
EDC
FDC
FDC
FDC
谢谢
您通常会使用递归查询:
with recursive cte as (
select type, cnt from mytable t
union all
select type, cnt - 1 from cte where cnt > 1
)
select type from cte
这里是a demo;该语法适用于 Postgres 和 MySQL 8.0.
在 Postgres 中你可以使用 generate_series()
:
insert into target_table (some_column)
select t.type
from the_table t
cross join generate_series(1,t."count")
order by t.type
TYPE COUNT
--- --
ABC 3
EDC 4
FDC 2
我想在 table 中使用单个 SQL 插入三次 ABC,四次 EDC,两次 FDC,可以吗? 查询的输出应使用以下条目插入到以下 table 中。
TYPE
----
ABC
ABC
ABC
EDC
EDC
EDC
EDC
FDC
FDC
FDC
谢谢
您通常会使用递归查询:
with recursive cte as (
select type, cnt from mytable t
union all
select type, cnt - 1 from cte where cnt > 1
)
select type from cte
这里是a demo;该语法适用于 Postgres 和 MySQL 8.0.
在 Postgres 中你可以使用 generate_series()
:
insert into target_table (some_column)
select t.type
from the_table t
cross join generate_series(1,t."count")
order by t.type