早教吧 育儿知识 作业答案 考试题库 百科 知识分享

select1frombwhereb.id=a.id和select1fromawhereb.id=a.id有区别么?请问他们有区别么?举个例子:select*frombwhereexists(select1fromawherea.c=b.c)select*frombwhereexists(select1frombwherea.c=

题目详情
select 1 from b where b.id = a.id和 select 1 from a where b.id = a.id有区别么?
请问他们有区别么?
举个例子:
select * from b where exists (select 1 from a where a.c=b.c)
select * from b where exists (select 1 from b where a.c=b.c)
这两句有区别么?
▼优质解答
答案和解析
选的表不同,你的例子,第一个是从b表选a.c=b.c的数据,第二个是从a表选。就好像a有3个c=1,b有1个c=1,你说从不同表选c=1结果一样不?就这意思。
其实你的:select * from b where exists (select 1 from a where a.c=b.c)
等价于:select * from b where b.c IN (select a.c from a where a.c=b.c)
可能用下面这句容易看懂些。就是从b表中查找与a表有相同字段c的所有结果集。