SQL更新语句更新错误的字段

时间:2016-10-25 22:39:48

标签: postgresql

我在Postgres中有以下代码

select op.url from identity.legal_entity le
join identity.profile op on le.legal_entity_id =op.legal_entity_id     
where op.global_id = '8wyvr9wkd7kpg1n0q4klhkc4g' 

返回1行。

然后我尝试使用以下内容更新url字段:

update identity.profile
set url = 'htpp:sam' 
where identity.profile.url in (
select op.url from identity.legal_entity le
join identity.profile op on le.legal_entity_id =op.legal_entity_id     
where global_id = '8wyvr9wkd7kpg1n0q4klhkc4g'
);

但是上面的内容最终会更新超过1行,实际上是身份表的所有行。

我会假设,因为第一个postgres语句返回一行,最多只能更新一行,但是我得到了更新所有行的错误效果。为什么?请帮助nubie修复上述更新声明。

1 个答案:

答案 0 :(得分:0)

不使用pip install wordcloud 标识要更新的行,而是使用主键。这就是它的用途。

因此,如果主键列名为profile.url,则语句可以修改为:

id

但是你可以在PostgreSQL中使用

更简单地做到这一点
UPDATE identity.profile
SET ...
WHERE identity.profile.id IN (SELECT op.id FROM ...);
相关问题