关于IN,EXISTS的使用问题,请教了

踏雪听雨 2010-08-11 08:37:53
如下两个语句,有什么区别,会带来什么问题?
selet * from t1 where t1.id in (select id from t2 where t2.name like '%test%')

selet * from t1 where exists (select id from t2 where t2.name like '%test%')
...全文
76 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuoxingyu 2010-08-13
  • 打赏
  • 举报
回复
结果不一样,无法评估效率。
wwwwb 2010-08-12
  • 打赏
  • 举报
回复
两个语句结果不一致
2只要存在t2.name like '%test%',则返回全部记录
1返回ID 在 (select id from t2 where t2.name like '%test%')中的记录
vga 2010-08-12
  • 打赏
  • 举报
回复
结果不一样!!
mysqi 2010-08-12
  • 打赏
  • 举报
回复
不等价的语句,执行结果也会不一样,这是本质的区别
feixianxxx 2010-08-11
  • 打赏
  • 举报
回复
selet * from t1 where t1.id in (select id from t2 where t2.name like '%test%')

selet * from t1 where exists (select id from t2 where t2.name like '%test%')

==>

第一个语句是要将2表关联起来 条件是 2表的id字段 只能返回t1的部分记录或者全部记录
第二个语句是只要(select id from t2 where t2.name like '%test%')为真 就是存在记录 那么就返回整个t1表的记录

2个语句是不一样的
ACMAIN_CHM 2010-08-11
  • 打赏
  • 举报
回复
MYSQL会使用不同的机制来执行。速度上会有差别。

57,063

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧