在 Postgresql 中使用第二个 table 的信息和第三个 table 的条件更新 table
Update a table using info from a second table and a condition from a third table in Postgresql
我现在的代码如下
UPDATE product_template
SET
listprice = product_uom.factor * product_template.list_price
FROM product_uom
INNER JOIN product_template ON product_uom.id = product_template.uom_id
INNER JOIN product_product ON product_product.id = product_template.id
WHERE product_product.manufacturer = 2646
据我了解,第 1 行指定了我要更新的 table。
然后我指定我想使用 2 个数字列更新 product_template 中名为 list_price 的列。
我指定第二个 table,其中有一列包含我需要的数值,以便从将要更新的 table 更新我的列。
我指定了将要更新的 table 的内部连接和包含我需要更新的信息的 table 。
我加入将要更新的 table 和 table ,其中有一列我需要作为更新发生的条件。
最后一行说明了为了在该行中发生更新而必须满足的条件。
实际上,如果我尝试在 postgresql 中 运行 这段代码,我会收到以下错误
错误:table 名称 "product_template" 指定了不止一次
我只使用 product_template 来指定将更新哪个 tabkle 以及两次以上来创建内部连接,在使用来自 2 个不同 tables?
读这个link:http://www.postgresql.org/docs/9.1/static/sql-update.html
from_list
A list of table expressions, allowing columns from other
tables to appear in the WHERE condition and the update expressions.
This is similar to the list of tables that can be specified in the
FROM Clause of a SELECT statement.
Note that the target table must not appear in the from_list, unless you intend a self-join
(in which
case it must appear with an alias in the from_list).
您需要使用别名为查询中的第二个 product_template
分配不同的名称,并在要引用第二个的地方用此别名替换 product_template
table.
PostgreSql 不知道您在查询的以下位置(第一个或第二个)指的是哪个 product_template
。我也不知道。
UPDATE product_template
SET ....... * product_template.list_price
...........
INNER JOIN product_template ON ...... = product_template.uom_id
INNER JOIN .......... ON ...... = product_template.id
遗憾的是,您不能使用 JOIN...ON
语法加入正在更新到其余 table 的 table。您必须将这些连接条件移动到 WHERE
子句中,并从 FROM
子句中删除更新后的 table 本身 product_template
。
UPDATE product_template
SET
list_price = product_uom.factor * product_template.list_price
FROM product_uom, product_product
WHERE product_product.manufacturer = 2646 and
product_uom.id = product_template.uom_id and
product_product.id = product_template.id
(这是假设您 不 想要进行自联接,而您似乎不想进行。如果您确实想要自联接,则需要分配一个 table 别名,就像非更新自连接一样。)
我现在的代码如下
UPDATE product_template
SET
listprice = product_uom.factor * product_template.list_price
FROM product_uom
INNER JOIN product_template ON product_uom.id = product_template.uom_id
INNER JOIN product_product ON product_product.id = product_template.id
WHERE product_product.manufacturer = 2646
据我了解,第 1 行指定了我要更新的 table。 然后我指定我想使用 2 个数字列更新 product_template 中名为 list_price 的列。 我指定第二个 table,其中有一列包含我需要的数值,以便从将要更新的 table 更新我的列。 我指定了将要更新的 table 的内部连接和包含我需要更新的信息的 table 。 我加入将要更新的 table 和 table ,其中有一列我需要作为更新发生的条件。 最后一行说明了为了在该行中发生更新而必须满足的条件。
实际上,如果我尝试在 postgresql 中 运行 这段代码,我会收到以下错误 错误:table 名称 "product_template" 指定了不止一次
我只使用 product_template 来指定将更新哪个 tabkle 以及两次以上来创建内部连接,在使用来自 2 个不同 tables?
读这个link:http://www.postgresql.org/docs/9.1/static/sql-update.html
from_list
A list of table expressions, allowing columns from other tables to appear in the WHERE condition and the update expressions. This is similar to the list of tables that can be specified in the FROM Clause of a SELECT statement.
Note that the target table must not appear in the from_list, unless you intend a self-join
(in which case it must appear with an alias in the from_list).
您需要使用别名为查询中的第二个 product_template
分配不同的名称,并在要引用第二个的地方用此别名替换 product_template
table.
PostgreSql 不知道您在查询的以下位置(第一个或第二个)指的是哪个 product_template
。我也不知道。
UPDATE product_template
SET ....... * product_template.list_price
...........
INNER JOIN product_template ON ...... = product_template.uom_id
INNER JOIN .......... ON ...... = product_template.id
遗憾的是,您不能使用 JOIN...ON
语法加入正在更新到其余 table 的 table。您必须将这些连接条件移动到 WHERE
子句中,并从 FROM
子句中删除更新后的 table 本身 product_template
。
UPDATE product_template
SET
list_price = product_uom.factor * product_template.list_price
FROM product_uom, product_product
WHERE product_product.manufacturer = 2646 and
product_uom.id = product_template.uom_id and
product_product.id = product_template.id
(这是假设您 不 想要进行自联接,而您似乎不想进行。如果您确实想要自联接,则需要分配一个 table 别名,就像非更新自连接一样。)