求一简单的SQL语句

marcus008 2008-02-26 12:04:23
有两张表:

表一:
id name
1 a
2 b
3 c


表二:(tid的值是根据表一中的id来的,即a >> a1)

id tid name
1 1 a1
2 1 a2
3 2 b1
4 2 b2


现在需要在表一中找出表二中仅有的子数据,即显示:
(表一中id为3的数据因为在表二没有显示出来,所以要舍掉,记得以前好像可以用个存在来查询这种情况的,不知怎么用了)

id name
1 a
2 b




这样的SQL语句,要如何写,谢谢..










...全文
101 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2008-02-26
  • 打赏
  • 举报
回复
select a.*
from table1 a
left join (select tid from table2 group by tid) b
on a.id = b.tid


select *
from table1 a
where exists(select 1 from table2 where tid = a.id)
pt1314917 2008-02-26
  • 打赏
  • 举报
回复

--写错了。不好意思:

select distinct a.* from 表一 a , 表二 b where a.id=b.tid
--或者
select distinct a.* from 表一 a left join 表二 b on a.id=b.tid where b.tid is not null
pt1314917 2008-02-26
  • 打赏
  • 举报
回复

select a.* from 表一 a left join 表二 b on a.id=b.tid and b.tid is not null
gahade 2008-02-26
  • 打赏
  • 举报
回复
select * from 表一 where id in (select distinct tid from 表二)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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