我需要在我的数据库中插入一大堆东西
写出来的是查询将是这样的事情(但有更多的东西!)
Insert into MyTable (country_id, event) values (1001, 'up')
Insert into MyTable (country_id, event) values (1001, 'down')
Insert into MyTable (country_id, event) values (1002, 'up')
Insert into MyTable (country_id, event) values (1003, 'down')
....
Insert into MyTable (country_id, event) values (9999, 'up')
Insert into MyTable (country_id, event) values (9999, 'down')
Run Code Online (Sandbox Code Playgroud)
我可以编写一些神奇的SQL连接/合并奇迹查询,它将获得所有country_ids和所有"事件"类型并创建我需要的数百个插入语句吗?
编辑:
有一张国家/地区表.哪个应插入到MyTable中是sql查询的结果,该查询的每个结果都需要MyTable中的"向上"和"向下"行.有足够的,我不想手动策划查询.
实际上,除了"向上"和"向下"之外,它们可以手动输入,或者作为进一步查询的结果.
创建国家和事件列表的笛卡尔积,并立即插入它们:
insert into MyTable (country_id, event)
select countries.country_id,
e.[event]
from countries
cross join
(
select 'Up' [event]
union all
select 'Down'
) e
Run Code Online (Sandbox Code Playgroud)