Postgres CTE查询
Postgres CTE query
- 我想在返回 id1 的表 1 中插入一个新行。
- 然后使用 id1 插入行到 table2 返回 id2。
- 然后用 id2 更新 table1 以创建关系。
- 最后,同时使用id1和id2插入到table3中。
我已经创建了一个 CTE,但它似乎没有执行第三步(使用 id2 更新 table1)。我怎样才能让它正确地完成所有步骤?
with
supply as (
insert into "MOBILE_SUPPLIES" ("NAME", "TYPE") values ('Test2', 1) returning *
),
loc as (
insert into "MOBILE_LOCATIONS" ("LNG", "LAT", "SUPPLY_ID") values (31.232, 31.232, (select "ID" from supply)) returning *
),
up as (
update "MOBILE_SUPPLIES" set "LOCATION_ID"=(select "ID" from loc) where "ID"=(select "ID" from supply) returning *
)
insert into "MOBILE_DATES" ("DATE_FROM", "LOCATION_ID", "SUPPLY_ID") values (current_timestamp, (select "ID" from loc), (select "ID" from supply))
澄清一下,我的 ID 可以通过“ID”访问(它需要双引号,我知道这不常见)
我认为您的第三步应该是更新自:
update "MOBILE_SUPPLIES"
set "LOCATION_ID"= my_data.loc_id
from (select loc."ID" as loc_id ,
supply."ID" as supply_id
from loc
inner join supply on loc."SUPPLY_ID" = supply."ID") as my_data
on my_data.supply_id="MOBILE_SUPPLIES"."ID"
returning *
如 the documentation 中所述,修改您在同一语句中添加的行将不起作用:
The sub-statements in WITH
are executed concurrently with each other and with the main query. Therefore, when using data-modifying statements in WITH
, the order in which the specified updates actually happen is unpredictable. All the statements are executed with the same snapshot (see Chapter 13), so they cannot “see” one another's effects on the target tables. This alleviates the effects of the unpredictability of the actual order of row updates, and means that RETURNING
data is the only way to communicate changes between different WITH
sub-statements and the main query.
我想这个问题是关于自动生成标识符的,"MOBILE_SUPPLIES"
和 "MOBILE_LOCATIONS"
中的行应该相互引用。
您可以通过显式获取序列值来做到这一点:
WITH new_loc_id AS (
SELECT nextval('"MOBILE_LOCATIONS_ID_seq"') AS "ID"
),
WITH supply AS (
INSERT INTO "MOBILE_SUPPLIES" ("NAME", "TYPE", "LOCATION_ID")
SELECT 'Test2', 1, "ID"
FROM new_loc_id
RETURNING "ID", "LOCATION_ID"
),
loc AS (
INSERT INTO "MOBILE_LOCATIONS" ("ID", "LNG", "LAT", "SUPPLY_ID")
SELECT "LOCATION_ID", 31.232, 31.232, "ID"
FROM supply
RETURNING "ID"
)
...
- 我想在返回 id1 的表 1 中插入一个新行。
- 然后使用 id1 插入行到 table2 返回 id2。
- 然后用 id2 更新 table1 以创建关系。
- 最后,同时使用id1和id2插入到table3中。 我已经创建了一个 CTE,但它似乎没有执行第三步(使用 id2 更新 table1)。我怎样才能让它正确地完成所有步骤?
with
supply as (
insert into "MOBILE_SUPPLIES" ("NAME", "TYPE") values ('Test2', 1) returning *
),
loc as (
insert into "MOBILE_LOCATIONS" ("LNG", "LAT", "SUPPLY_ID") values (31.232, 31.232, (select "ID" from supply)) returning *
),
up as (
update "MOBILE_SUPPLIES" set "LOCATION_ID"=(select "ID" from loc) where "ID"=(select "ID" from supply) returning *
)
insert into "MOBILE_DATES" ("DATE_FROM", "LOCATION_ID", "SUPPLY_ID") values (current_timestamp, (select "ID" from loc), (select "ID" from supply))
澄清一下,我的 ID 可以通过“ID”访问(它需要双引号,我知道这不常见)
我认为您的第三步应该是更新自:
update "MOBILE_SUPPLIES"
set "LOCATION_ID"= my_data.loc_id
from (select loc."ID" as loc_id ,
supply."ID" as supply_id
from loc
inner join supply on loc."SUPPLY_ID" = supply."ID") as my_data
on my_data.supply_id="MOBILE_SUPPLIES"."ID"
returning *
如 the documentation 中所述,修改您在同一语句中添加的行将不起作用:
The sub-statements in
WITH
are executed concurrently with each other and with the main query. Therefore, when using data-modifying statements inWITH
, the order in which the specified updates actually happen is unpredictable. All the statements are executed with the same snapshot (see Chapter 13), so they cannot “see” one another's effects on the target tables. This alleviates the effects of the unpredictability of the actual order of row updates, and means thatRETURNING
data is the only way to communicate changes between differentWITH
sub-statements and the main query.
我想这个问题是关于自动生成标识符的,"MOBILE_SUPPLIES"
和 "MOBILE_LOCATIONS"
中的行应该相互引用。
您可以通过显式获取序列值来做到这一点:
WITH new_loc_id AS (
SELECT nextval('"MOBILE_LOCATIONS_ID_seq"') AS "ID"
),
WITH supply AS (
INSERT INTO "MOBILE_SUPPLIES" ("NAME", "TYPE", "LOCATION_ID")
SELECT 'Test2', 1, "ID"
FROM new_loc_id
RETURNING "ID", "LOCATION_ID"
),
loc AS (
INSERT INTO "MOBILE_LOCATIONS" ("ID", "LNG", "LAT", "SUPPLY_ID")
SELECT "LOCATION_ID", 31.232, 31.232, "ID"
FROM supply
RETURNING "ID"
)
...