update demo_order a set order_name='橘子' where exists (
select 1 from demo_table b where a.order_id=b.order_id
);
原始数据:
执行语句后的数据:
以上语句可以这样理解,在demo_order中满足两表id相等这一条件的所有行的order_name变为橘子。换句话来讲就是exists作为一个判断条件,如果后面的语句成立,则做相应操作。
update demo_order a set order_name='梨' where not exists (
select 1 from demo_table b where a.order_id=b.order_id
);
执行后:
这一语句恰是第一个语句的反面,就是把不满足条件的语句进行相应操作。

本文介绍了如何使用SQL语句通过exists和not exists子句来更新表中的数据。exists子句用于将符合条件的数据更新为指定值,而not exists则用于更新不符合条件的数据。



872

被折叠的 条评论
为什么被折叠?



