update 后面带exists的作用是什么?不带的话,有什么区别?

[复制链接]
查看11 | 回复4 | 2021-9-12 18:20:52 | 显示全部楼层 |阅读模式
update t2 set t2.c3 = (select t1.id from t1 where t1.c1 = t2.c1 and t1.c2 = t2.c2)
where exists(select t1.id from t1 where t1.c1 = t2.c1 and t1.c2 = t2.c2);

回复

使用道具 举报

千问 | 2021-9-12 18:20:52 | 显示全部楼层
不带exists时,则将所有的t2.c3更新成t1.id。带exists时,则判断t1.id是否为空,如果为空则在1的基础上将t2.c3更新为空。EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False。EXISTS指定一个子查询,检测行的存在。in和exists的区别exists:存在。exists()后面的子查询不返回列表的值,只是返回一个ture或false的结果,也就是它只在乎括号里的数据能不能查找出来,是否存在这样的记录。其运行方式是
回复

使用道具 举报

千问 | 2021-9-12 18:20:52 | 显示全部楼层
update 后面带exists:只更新符合条件的值。不带Exists:更新所有的t2.c3。(select t1.id from t1 where t1.c1 = t2.c1 and t1.c2 = t2.c2) 不存在的话,将更新为空值。
回复

使用道具 举报

千问 | 2021-9-12 18:20:52 | 显示全部楼层
带exists:只更新符合exists内子查询条件的数据,不带exists:更新所有t2表中的数据,当子查询有值时,更新为t1.id,当子查询无值时,更新为空;
回复

使用道具 举报

千问 | 2021-9-12 18:20:52 | 显示全部楼层
1、不带exists时,则将所有的t2.c3更新成t1.id;
注:t1需满足 t1.c1 = t2.c1 and t1.c2 = t2.c22、带exists时,则判断t1.id是否为空,如果为空则在1的基础上将t2.c3更新为空; 注:exists(select t1.id from t1 where t1.c1 = t2.c1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行