基于文档 https://www.postgresql.org/docs/9.1/static/sql-update.html 我不可能要做什么(在该页面的最底部(
====
============UPDATE accounts SET (contact_last_name, contact_first_name) =
(SELECT last_name, first_name FROM salesmen
WHERE salesmen.id = accounts.sales_id);
====
============有没有其他方法可以做到这一点?
db=> update "Template" set ("MsgCategoryName","FromAddress","FromName","ToAddress","ToName","BccAddress","EmailSubject","EmailHTML","EmailPlanText") = (Select "MsgCategoryName","FromAddress","FromName","ToAddress","ToName","BccAddress","EmailSubject","EmailHTML","EmailPlanText" from "Template" where "AccountID" = 1016020479 and "LocaleID" = 'nl' and "TmplName" = 'Invoice/Nobrand/PDF') where "AccountID" = 1017069459 and "TmplName" = 'Invoice/Nobrand/PDF' and "LocaleID" = 'nl';
ERROR: syntax error at or near "Select"
LINE 1: ...s","EmailSubject","EmailHTML","EmailPlanText") = (Select "Ms...
db=> select version();
version
-------------------------------------------------------------------------------------------------------------------
PostgreSQL 9.0.13 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3), 64-bit
(1 row)
^
我没有这个旧版本要检查,但我相信你不能使用 CTE 进行更新(你可以肯定使用 9.1(,在这种情况下你可以 plpgsql 它:
do
$$
declare _r record;
begin
for _r in (SELECT id,last_name, first_name FROM salesmen JOIN accounts on salesmen.id = accounts.sales_id) loop
UPDATE accounts SET contact_last_name = _r.last_name, contact_first_name = _r.first_name where sales_id = _r.id;
end loop;
end;
$$
;