因此,我有以下代码,该代码根据id和语言代码更新语言字符串表。如果我使用相同的from表达式执行SELECT,它只会选择一行。如果我进行更新,它将更新所有行。我在哪里出错了?
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';
这是两个表的定义:
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)
);
答案 0 :(得分:2)
不要重复FROM
中正在更新的表。所以:
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';
在Postgres中,对表的每个引用都是独立的。您的更新与此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';
这绝对不是您想要的。