2015-06-06 46 views
0

我当前的代码如下所示更新第二表格从第三表使用表信息和状态在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个指定要更新的表。 然后我指定我想使用2个数字列更新product_template中名为list_price的列。 我指定有一个数值,我需要以更新来自将要被更新的表我的专栏,列第二个表。 我指定将要更新的表和具有我需要做这样的信息表的内连接。 我加入一个是要与有一栏,我需要为更新发生的条件表中可以更新的表。 最后一行指出为了使更新在该行中发生而必须满足的条件。

正因为如此,如果我尝试运行PostgreSQL的这段代码我碰到下面的错误 错误:表名“product_template”指定不止一次

我只用product_template指定哪些tabkle将更新,再次创建内部连接两次,在使用来自2个不同表的信息时更新此表的正确方法是什么?

回答

0

不幸的是,你不能使用JOIN...ON语法加入到正在更新的桌子上,其余的表。你必须移动的加入条件为WHERE条款,并从FROM条款删除更新表本身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 

(这是假设你做希望做一个自我加盟,这似乎不这样做。如果你想自连接,那么你需要指定一个表别名,只需喜欢与不更新自联接。)

+0

作品,被接受为答案。 – Marcus

0

阅读此链接: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
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 
+0

这是正确的,你thankl。作品被接受为作品。 – Marcus

相关问题