一个用查询结果再次查询的问题!

ThankU 2008-12-15 05:51:46
select distinct id
from table1
where name='ZHANG'

比如会返回
id
001
002
004

然后我想用查到的id组再查找table2,返回在table2里存在的id组?(最好不用临时表)
比如
id
001
004

(比如id=002的数据不存在于table2里面)

...全文
50 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ws_hgo 2008-12-15
  • 打赏
  • 举报
回复
select id from table2  where id in(select distinct id 
from table1
where name='ZHANG'
)
mengxj85 2008-12-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fcuandy 的回复:]
select * from table2 a where exists(select 1 from table1 where name='ZHANG' and id=a.id)

select a.* from table2 a inner join table1 b on a.id=b.id where b.name='ZHANG'

写法比较多,优先这两种吧。
[/Quote]
  • 打赏
  • 举报
回复
select a.* from table2 a inner join table1 b on a.id=b.id where b.name='ZHANG'
百年树人 2008-12-15
  • 打赏
  • 举报
回复
select b.id
from table1 a,table2 b
where a.id=b.id
and a.id='ZHANG'
ljhcy99 2008-12-15
  • 打赏
  • 举报
回复
select * from table2
where id not in(
select distinct id
from table1
where name='ZHANG')
AndrewITBOY 2008-12-15
  • 打赏
  • 举报
回复

select *
from table2
left join (select distinct id
from table1
where name='ZHANG'
) A on A.id = table2.id
where 1=1
fcuandy 2008-12-15
  • 打赏
  • 举报
回复
select * from table2 a where exists(select 1 from table1 where name='ZHANG' and id=a.id)

select a.* from table2 a inner join table1 b on a.id=b.id where b.name='ZHANG'

写法比较多,优先这两种吧。
csdyyr 2008-12-15
  • 打赏
  • 举报
回复
select * from 
(select distinct id
from table1
where name='ZHANG' ) a join table2 as b
on a.id=b.id

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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