我必须运行多个相互依赖的查询
select a from a ( result has multiple rows)
select b from b ( result has multiple rows)
对于该查询中的每一行,我需要运行此
select c from c where c.a=a amd c.b=b
insert c into d
有点像:
select a from a ( result has multiple rows)
select b from b ( result has multiple rows
for a in a
for b in b
select c from c where c.a=a amd c.b=b
insert c,a,b into d
这可以作为存储过程吗?
谢谢
答案 0 :(得分:4)
存储过程?当然。拍摄,甚至可以在一个声明中说明:
INSERT INTO d (c)
SELECT c FROM c WHERE c.a IN (SELECT a FROM a) AND c.b IN (SELECT b FROM b)
你甚至可以通过连接做得更好,但写一下我需要更多地了解这些表的关系。
答案 1 :(得分:3)
我很确定你可以在不使用SQL中的任何循环的情况下编写此查询。
答案 2 :(得分:1)
INSERT
INTO d
SELECT c.*
FROM a
CROSS JOIN
b
JOIN с
ON c.a = a.id
AND c.b = b.id