PostgreSQL更新的行多于where条件?

时间:2015-01-04 04:38:27

标签: sql postgresql

我有以下简单的SQL更新:

update t2 set col = 2 
from t2, t1
where t2.id = t1.id
    and t1.name = 'test';

但是t2中的所有行都会更新!即不仅仅id name {{1}}是'测试',我如何在Postgres实现这一目标?

2 个答案:

答案 0 :(得分:2)

from t1,t2过多了 这样做:

update t2 set col = 2 
from t1
where t2.id = t1.id
    and t1.name = 'test';

如果你必须在别名中加入t2并加入where ..

中的所有三个表
update t2 as t2_up set col = 2 
from t2 as t2_from , t1
where t2_from.id = t1.id
    and t2_up.id = t2_from.id
    and t1.name = 'test';

答案 1 :(得分:1)

我猜你的意思是:

update t2
    set col = 2 
from t2 join
     t1
     on t1.id = t2.id
where t1.name = 'test';

条件t1.id = t1.id在查询中没有多大意义。它相当于t1.id is not null。因此,t2中的所有行都会因交叉连接而更新。