PostgreSQL INSERT INTO table 从多个选择中获取的多条记录
PostgreSQL INSERT INTO table multiple records taken from multiple selects
我有这3个tables
食谱:recipe_id | name
成分:ingredient_id | name
recipes_ingredients: id | recipe_id | ingredient_id
每个table的第一个id是SERIAL PRIMARY KEY
,两个名字是character varying(50)
。我正在尝试在 recipes_ingredients
中插入 recipe_id
和 ingredient_id
,如果我使用单一成分进行操作,效果会很好。问题是我不知道如何插入与单个食谱关联的多种成分。
这是我尝试插入与同一食谱相关的 3 种不同成分的内容:
BEGIN;
WITH new_recipe AS (
INSERT INTO recipe (name) VALUES ('{}') RETURNING recipe_id
), ingredient1 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
), ingredient2 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
), ingredient3 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
)
INSERT INTO recipes_ingredients (recipe_id, ingredient_id)
SELECT new_recipe.recipe_id, ingredient1.ingredient_id FROM new_recipe CROSS JOIN ingredient1,
SELECT new_recipe.recipe_id, ingredient2.ingredient_id FROM new_recipe CROSS JOIN ingredient2,
SELECT new_recipe.recipe_id, ingredient3.ingredient_id FROM new_recipe CROSS JOIN ingredient3
COMMIT;
它给我这个错误:
ERROR: syntax error at or near "SELECT"
LINE 13: SELECT new_recipe.recipe_id, ingredient2.ingredient_id FROM...
^
SQL state: 42601
Character: 520
尝试在每个 select
之后用 union/union all
替换 ,
BEGIN;
WITH new_recipe AS (
INSERT INTO recipe (name) VALUES ('{}') RETURNING recipe_id
), ingredient1 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
), ingredient2 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
), ingredient3 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
)
INSERT INTO recipes_ingredients (recipe_id, ingredient_id)
SELECT new_recipe.recipe_id, ingredient1.ingredient_id FROM new_recipe CROSS JOIN ingredient1 Union
SELECT new_recipe.recipe_id, ingredient2.ingredient_id FROM new_recipe CROSS JOIN ingredient2 Union
SELECT new_recipe.recipe_id, ingredient3.ingredient_id FROM new_recipe CROSS JOIN ingredient3;
COMMIT;
我有这3个tables
食谱:recipe_id | name
成分:ingredient_id | name
recipes_ingredients: id | recipe_id | ingredient_id
每个table的第一个id是SERIAL PRIMARY KEY
,两个名字是character varying(50)
。我正在尝试在 recipes_ingredients
中插入 recipe_id
和 ingredient_id
,如果我使用单一成分进行操作,效果会很好。问题是我不知道如何插入与单个食谱关联的多种成分。
这是我尝试插入与同一食谱相关的 3 种不同成分的内容:
BEGIN;
WITH new_recipe AS (
INSERT INTO recipe (name) VALUES ('{}') RETURNING recipe_id
), ingredient1 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
), ingredient2 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
), ingredient3 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
)
INSERT INTO recipes_ingredients (recipe_id, ingredient_id)
SELECT new_recipe.recipe_id, ingredient1.ingredient_id FROM new_recipe CROSS JOIN ingredient1,
SELECT new_recipe.recipe_id, ingredient2.ingredient_id FROM new_recipe CROSS JOIN ingredient2,
SELECT new_recipe.recipe_id, ingredient3.ingredient_id FROM new_recipe CROSS JOIN ingredient3
COMMIT;
它给我这个错误:
ERROR: syntax error at or near "SELECT"
LINE 13: SELECT new_recipe.recipe_id, ingredient2.ingredient_id FROM...
^
SQL state: 42601
Character: 520
尝试在每个 select
之后用union/union all
替换 ,
BEGIN;
WITH new_recipe AS (
INSERT INTO recipe (name) VALUES ('{}') RETURNING recipe_id
), ingredient1 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
), ingredient2 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
), ingredient3 AS (
INSERT INTO ingredient (name) VALUES ('{}') RETURNING ingredient_id
)
INSERT INTO recipes_ingredients (recipe_id, ingredient_id)
SELECT new_recipe.recipe_id, ingredient1.ingredient_id FROM new_recipe CROSS JOIN ingredient1 Union
SELECT new_recipe.recipe_id, ingredient2.ingredient_id FROM new_recipe CROSS JOIN ingredient2 Union
SELECT new_recipe.recipe_id, ingredient3.ingredient_id FROM new_recipe CROSS JOIN ingredient3;
COMMIT;