使用公共表表达式更新
我有一对处于一对多关系的表。我想在一个表中插入行,在多个表中插入行,然后在一个表中更新新行。
简化的结构如下。有一个小提琴 https://dbfiddle.uk/?rdbms=postgres_12&fiddle=8555fb74372b91c8854f7a18e78110c0
除了最后一步(应该更新新行)之外,一切正常。我得到更新值的NULL
。
我看不出要让它发挥作用还缺少什么。我怀疑这与使用 FROM
子句更新的正确语法有关。根据文档,这就是如何向 UPDATE 语句提供附加数据的方法。另一方面,可能还有更根本的东西。
我知道我可以编写一个函数来做到这一点,而且我已经做到了。这里我想更好地理解涉及到的SQL。
CREATE TABLE testsales(
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
customerid int,
total decimal(6,2)
);
CREATE TABLE testitems(
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
saleid int REFERENCES testsales(id),
productid int,
quantity int,
price decimal(5,2)
);
with
customer(id) as (select 562),
items(productid,quantity) as (values(1489,2),(746,1),(1093,3)),
newsale as (
insert into testsales(customerid)
select id FROM customer
returning id
),
addsaleitems as(
insert into testitems(saleid,productid,quantity,price)
select
newsale.id, items.productid, items.quantity,100
from newsale, items
returning *
)
update testsales
set total=(select sum(price*quantity) from addsaleitems)
from addsaleitems,newsale
where testsales.id=newsale.id;
在上面的示例中,前两个 CTE,customer
和 items
只是为后面的内容提供值。 newsale
CTE 在 One 表中创建新行,addsaleitems
将行添加到 Many
表中,其余部分应该更新一张桌子。
I have a pair of tables in a one to many relationship. I want to insert rows in the One table, rows in the Many table, and then update the new row in the One table.
A simplified structure is as below. There is a fiddle at https://dbfiddle.uk/?rdbms=postgres_12&fiddle=8555fb74372b91c8854f7a18e78110c0
Everything works except for the last step, where the new rows is supposed to be updated. I get a NULL
for the updated value.
I can’t see what is missing to get this to work. I suspect it has something to do with the correct syntax to update using the FROM
clause. According to the documentation, that’s how you can supply additional data to the UPDATE
statement. On the other hand, there may be something more fundamental.
I know that I can write a function to do this, and I have already done that. Here I want to get a better understanding of the SQL involved.
CREATE TABLE testsales(
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
customerid int,
total decimal(6,2)
);
CREATE TABLE testitems(
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
saleid int REFERENCES testsales(id),
productid int,
quantity int,
price decimal(5,2)
);
with
customer(id) as (select 562),
items(productid,quantity) as (values(1489,2),(746,1),(1093,3)),
newsale as (
insert into testsales(customerid)
select id FROM customer
returning id
),
addsaleitems as(
insert into testitems(saleid,productid,quantity,price)
select
newsale.id, items.productid, items.quantity,100
from newsale, items
returning *
)
update testsales
set total=(select sum(price*quantity) from addsaleitems)
from addsaleitems,newsale
where testsales.id=newsale.id;
In the above sample, the first two CTEs, customer
and items
are simply there to provide values for what follows. The newsale
CTE creates the new row in the One table, addsaleitems
adds rows to the Many
table, and the rest of it is supposed to update the One table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据记录,
更新
尚无法看到同一语句中插入的行。诀窍是在将行插入
testsales
时首先计算总和。As documented, the
UPDATE
cannot yet see the rows inserted in the same statement.The trick is to calculate the sum first, right when you insert the row into
testsales
.