不客套了,碰到一个郁闷的问题
两个表
A表 Aname Adesc Age
B表 Bname Bdesc
需求:从A表中列出 存在于B表的记录,即列出A表中,年龄<30的记录,注意只列出A表的记录,B表不要
不要用select A.* from a,b where a.Age<30 and a.Aname in (select * from B) 此句如鸡肋,全表子查询太耗时间.各位同好,帮忙一下哦 。
两个表
A表 Aname Adesc Age
B表 Bname Bdesc
需求:从A表中列出 存在于B表的记录,即列出A表中,年龄<30的记录,注意只列出A表的记录,B表不要
不要用select A.* from a,b where a.Age<30 and a.Aname in (select * from B) 此句如鸡肋,全表子查询太耗时间.各位同好,帮忙一下哦 。
select A.*
from a left join b on A.Aname=B.Bname
where a.Age <30 and b.Bname is null
where a.age <30 and b.bname is not null
from a
where age<30
and exists (select 1 from b where b.bname=a.aname)
发现问题select *
from a
where age<30
and exists (select 1 from b where b.bname=a.aname)得到的是30笔记录select *
from a
where exists (select 1 from b where b.bname=a.aname)
得到的是27笔记录-----------------------------------------------
出现这样一种结果:
加了一个限制条件>30,却得出更多的记录
不解
3楼条件少了点:
应该:
select *
from a
where age <30
and exists (select 1 from b where b.bname=a.aname and age <30 )
select A.*
from a join b on A.Aname=B.Bname
where a.Age <30 --得到的记录呢?
我手误在exists里把 exists (select * from b...)写成了 exists (select * from a,b...),结果得到总是A表所有的记录
晕死,拖累各位在这里浪费了这么多时间,我自己也浪费了半天
楼上各方法均执行通过。感谢各位的热忱,结贴。。