我在我的公司使用Postgresql作为主要商店,我正在努力实现涉及三个不同表的更新查询。
这是架构:
我想更新表1中Table1.entity_type = 'SpecialCompany'
的所有行,以填充Table1.company_id
,以便:{/ p>
Table1.entity_id = SpecialCompany.id and SpecialCompany.company_id = Company.id
我已经开始这样的事了:
UPDATE Table1
SET company_id = (select c.id
FROM company c
INNER JOIN special_company w ON c.id=w.company_id
WHERE w.id=709)
WHERE entity_type='SpecialCompany' AND entity_id=709;
但我无法将所有709
替换为Table1.entity_id
{/ 1}}。
非常感谢任何帮助。
答案 0 :(得分:1)
我认为你正在寻找这样的东西。
update t
set t.company_id=c.id
from
Company C
INNER JOIN special_company w ON c.id=w.company_id
INNER JOIN Table1 t on t.entity_id=w.entity_id
Where t.entity_type = 'SpecialCompany'
答案 1 :(得分:1)
当我找到解决方案时,我正在回答我的问题:
UPDATE Table1 t
SET company_id = c.id
FROM Company c
INNER JOIN special_company w ON c.id=w.company_id
WHERE
t.entity_type = 'SpecialCompany' AND t.entity_id=w.id;
感谢@Krishna的帮助!