[英]Postgresql UPDATE LEFT JOIN
So, I have the following code that updates a language string table, based on an id, and a language code. 因此,我有以下代码根据ID和语言代码更新语言字符串表。 If I perform a SELECT using the same from expression, it selects just one row. 如果我使用相同的from表达式执行SELECT,它只会选择一行。 If I do update, it updates all rows. 如果我进行更新,它将更新所有行。 Where am I going wrong with this? 我在哪里错呢?
UPDATE shopmaster.catalog_lang SET shortname='TEST'
FROM shopmaster.catalog_lang cl LEFT JOIN shopmaster.lang l ON cl.langid=l.langid
WHERE cl.catalogid=7 AND l.code='fr';
Here's the definition of the two tables: 这是两个表的定义:
CREATE TABLE IF NOT EXISTS shopmaster.lang(
langid SERIAL,
name TEXT,
code TEXT,
active BOOLEAN,
donotdelete BOOLEAN,
PRIMARY KEY (langid)
CREATE TABLE IF NOT EXISTS shopmaster.catalog_lang(
catalogid INT references shopmaster.catalog(catalogid),
langid INT references shopmaster.lang(langid),
title TEXT,
shortname TEXT,
dirname TEXT,
PRIMARY KEY (catalogid, langid)
);
Don't repeat the table being updated in the FROM
. 不要重复在FROM
中更新的表。 So: 所以:
UPDATE shopmaster.catalog_lang cl
SET shortname = 'TEST'
FROM shopmaster.lang l
WHERE cl.langid = l.langid AND cl.catalogid = 7 AND l.code = 'fr';
In Postgres each reference to the table is separate. 在Postgres中,对该表的每个引用都是独立的。 Your update is equivalent to this SELECT
: 您的更新等效于此SELECT
:
SELECT . . .
FROM shopmaster.catalog_lang CROSS JOIN
shopmaster.catalog_lang cl LEFT JOIN
shopmaster.lang l
ON cl.langid = l.langid
WHERE cl.catalogid = 7 AND l.code = 'fr';
And this is definitely not what you intend. 这绝对不是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.