gpt4 book ai didi

postgresql - 返回子句总是先执行吗?

转载 作者:行者123 更新时间:2023-11-29 12:57:47 25 4
gpt4 key购买 nike

我有一个多对多关系,表示 container 持有 item

我在表中有一个主键 row_id。

我插入四行:(container_id, item_id) values (1778712425160346751, 4)。除了上述唯一的 row_id 之外,这些行将是相同的。

我随后执行以下查询:

delete from contains
where item_id = 4 and
container_id = '1778712425160346751' and
row_id =
(
select max(row_id) from contains
where container_id = '1778712425160346751' and
item_id = 4
)
returning
(
select count(*) from contains
where container_id = '1778712425160346751' and
item_id = 4
);

现在我希望从此查询返回 3,但我得到了 4。得到 4 是期望的行为,但这不是预期的结果。

我的问题是:我是否总是期望 returning 子句在删除之前执行,或者这是某些版本或特定软件的特性?

最佳答案

returning 部分中使用查询是允许的,但没有记录。对于 the documentation :

output_expression

An expression to be computed and returned by the DELETE command after each row is deleted. The expression can use any column names of the table named by table_name or table(s) listed in USING. Write * to return all columns.

查询看到表处于删除之前的状态似乎是合乎逻辑的,因为语句尚未完成。

create temp table test as 
select id from generate_series(1, 4) id;

delete from test
returning id, (select count(*) from test);

id | count
----+-------
1 | 4
2 | 4
3 | 4
4 | 4
(4 rows)

同样关注更新:

create temp table test as 
select id from generate_series(1, 4) id;

update test
set id = id+ 1
returning id, (select sum(id) from test);

id | sum
----+-----
2 | 10
3 | 10
4 | 10
5 | 10
(4 rows)

关于postgresql - 返回子句总是先执行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38669335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com