插入后如何 return WITH 子句中的另一个值
How to return another value from WITH clause after Inserting
我尝试使用 With 子句插入另一个 table。我可以 return 来自最终查询的 id,但主要目标是获取适配器的名称。
With AdapterName as (Select CONCAT('s', LTRIM(name, 's')::integer+1) as new_name
From adapter
Where adapter.name LIKE 's%'
Order by id desc
Limit 1),
AdapterIns as (INSERT INTO adapter (create_date,name,tag) SELECT now(),new_name, new_name From AdapterName RETURNING id,name)
INSERT INTO user_adapter (create_date, user_id, adapter_id)
Select now(), 1, AdapterIns.id
From AdapterIns
RETURNING adapter_id;
我怎样才能returnAdapterIns.name作为最终结果?
谢谢,
更新:我也尝试加入 adapter_id 与原始 table 但最新的 INSERT 出现以下错误:
[42601] ERROR: syntax error at or near "INSERT"
Select adapter.name
From adapter
Join (With AdapterName as (Select CONCAT('s', LTRIM(name, 's')::integer+1) as new_name
From adapter
Where adapter.name LIKE 's%'
Order by id desc
Limit 1),
AdapterIns as (INSERT INTO adapter (create_date,name,tag) SELECT now(),new_name, new_name From AdapterName RETURNING id)
INSERT INTO user_adapter (create_date, user_id, adapter_id)
Select now(), 1, AdapterIns.id
From AdapterIns
RETURNING adapter_id) temp on adapter.id = temp.adapter_id;
你不能。 Postgres 只允许您 return 插入的列。
可以用returning *
再加入回原来的table得到名字
也将第二个插入语句塑造为 CTE(我在下面称之为 original_insert
)然后选择将有所帮助。
With AdapterName as (Select CONCAT('s', LTRIM(name, 's')::integer+1) as new_name
From adapter
Where adapter.name LIKE 's%'
Order by id desc
Limit 1),
AdapterIns as (INSERT INTO adapter (create_date,name,tag) SELECT now(),new_name, new_name From AdapterName RETURNING id,name),
original_insert as (
INSERT INTO user_adapter (create_date, user_id, adapter_id)
Select now(), 1, AdapterIns.id
From AdapterIns
RETURNING adapter_id
)
SELECT oi.adapter_id, ai.name
FROM original_insert oi JOIN AdapterIns ai ON oi.adapter_id = ai.id;
我尝试使用 With 子句插入另一个 table。我可以 return 来自最终查询的 id,但主要目标是获取适配器的名称。
With AdapterName as (Select CONCAT('s', LTRIM(name, 's')::integer+1) as new_name
From adapter
Where adapter.name LIKE 's%'
Order by id desc
Limit 1),
AdapterIns as (INSERT INTO adapter (create_date,name,tag) SELECT now(),new_name, new_name From AdapterName RETURNING id,name)
INSERT INTO user_adapter (create_date, user_id, adapter_id)
Select now(), 1, AdapterIns.id
From AdapterIns
RETURNING adapter_id;
我怎样才能returnAdapterIns.name作为最终结果?
谢谢,
更新:我也尝试加入 adapter_id 与原始 table 但最新的 INSERT 出现以下错误:
[42601] ERROR: syntax error at or near "INSERT"
Select adapter.name
From adapter
Join (With AdapterName as (Select CONCAT('s', LTRIM(name, 's')::integer+1) as new_name
From adapter
Where adapter.name LIKE 's%'
Order by id desc
Limit 1),
AdapterIns as (INSERT INTO adapter (create_date,name,tag) SELECT now(),new_name, new_name From AdapterName RETURNING id)
INSERT INTO user_adapter (create_date, user_id, adapter_id)
Select now(), 1, AdapterIns.id
From AdapterIns
RETURNING adapter_id) temp on adapter.id = temp.adapter_id;
你不能。 Postgres 只允许您 return 插入的列。
可以用returning *
再加入回原来的table得到名字
也将第二个插入语句塑造为 CTE(我在下面称之为 original_insert
)然后选择将有所帮助。
With AdapterName as (Select CONCAT('s', LTRIM(name, 's')::integer+1) as new_name
From adapter
Where adapter.name LIKE 's%'
Order by id desc
Limit 1),
AdapterIns as (INSERT INTO adapter (create_date,name,tag) SELECT now(),new_name, new_name From AdapterName RETURNING id,name),
original_insert as (
INSERT INTO user_adapter (create_date, user_id, adapter_id)
Select now(), 1, AdapterIns.id
From AdapterIns
RETURNING adapter_id
)
SELECT oi.adapter_id, ai.name
FROM original_insert oi JOIN AdapterIns ai ON oi.adapter_id = ai.id;