使用公共表表达式更新

发布于 2025-01-09 07:19:40 字数 1629 浏览 0 评论 0原文

我有一对处于一对多关系的表。我想在一个表中插入行,在多个表中插入行,然后在一个表中更新新行。

简化的结构如下。有一个小提琴 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,customeritems 只是为后面的内容提供值。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏林苍穹下 2025-01-16 07:19:40

据记录更新 尚无法看到同一语句中插入的行。

诀窍是在将行插入 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文