了解With查询操作
Understanding With query operation
我在 postgresql 指南中看到了这个例子:
WITH t AS (
DELETE FROM foo
)
DELETE FROM bar;
手册说:
This example would remove all rows from tables foo and bar. The number
of affected rows reported to the client would only include rows
removed from bar.
如果我不这样做,为什么它会从 foo
中删除 "call" t
?
通常with
的用法如下:
WITH t AS (
UPDATE products SET price = price * 1.05
RETURNING *
)
SELECT * FROM t;
有一个对 t
的实际调用,然后才 "summons" with
块。
谁能解释一下?
引用的示例显示 with
子句中的查询无论是否在主查询中使用都会执行。根据这个原则,您可以在单个查询中执行多个独立的操作。这是一个非常方便的解决方案,大大扩展了 SQL.
的灵活性
请注意,如果您不想执行查询 t
,请不要使用它:
DELETE FROM bar;
如果你用过
WITH t AS (
DELETE FROM foo
)
DELETE FROM bar;
这意味着您打算在一个查询中从两个表 foo
和 bar
中删除行。
在the documentation中您可以找到:
If a data-modifying statement in WITH lacks a RETURNING clause, then
it forms no temporary table and cannot be referred to in the rest of
the query. Such a statement will be executed nonetheless.
我在 postgresql 指南中看到了这个例子:
WITH t AS (
DELETE FROM foo
)
DELETE FROM bar;
手册说:
This example would remove all rows from tables foo and bar. The number of affected rows reported to the client would only include rows removed from bar.
如果我不这样做,为什么它会从 foo
中删除 "call" t
?
通常with
的用法如下:
WITH t AS (
UPDATE products SET price = price * 1.05
RETURNING *
)
SELECT * FROM t;
有一个对 t
的实际调用,然后才 "summons" with
块。
谁能解释一下?
引用的示例显示 with
子句中的查询无论是否在主查询中使用都会执行。根据这个原则,您可以在单个查询中执行多个独立的操作。这是一个非常方便的解决方案,大大扩展了 SQL.
请注意,如果您不想执行查询 t
,请不要使用它:
DELETE FROM bar;
如果你用过
WITH t AS (
DELETE FROM foo
)
DELETE FROM bar;
这意味着您打算在一个查询中从两个表 foo
和 bar
中删除行。
在the documentation中您可以找到:
If a data-modifying statement in WITH lacks a RETURNING clause, then it forms no temporary table and cannot be referred to in the rest of the query. Such a statement will be executed nonetheless.