一个查询!

phyedu 2010-10-07 10:56:53
a表:
yhid(主)
1
2
3
b表:
yhid,s,d
1 s1,s2
1 s2,s3
2 s1,s2
c表:
yhid,s,d,
1 d1,d2
a表与b,c表都是1对多的关系,b表有的yhid,c表不一定有,同样,c表有的yhid,b表不一定有
给a表一个查询条件,希望得到
yhid,s,d
1 s1,s2
1 s2,s3
1 d1,d2
...全文
82 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Oraclers 2010-10-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 oraclers 的回复:]
或者:
select *
from (select b.* from b union select c.* from c) q join a on a.yhid=q.yhid
where a.yhid=1
[/Quote]
select q.*
from (select b.* from b union select c.* from c) q join a on a.yhid=q.yhid
where a.yhid=1
Oraclers 2010-10-07
  • 打赏
  • 举报
回复
或者:
select *
from (select b.* from b union select c.* from c) q join a on a.yhid=q.yhid
where a.yhid=1
Oraclers 2010-10-07
  • 打赏
  • 举报
回复
select b.yhid,b.s,b.d from a join b on a.yhid=b.yhid where a.yhid=1
union
select c.yhid,c.s,c.d from a join c on a.yhid=c.yhid where a.yhid=1
xiaoxiangqing 2010-10-07
  • 打赏
  • 举报
回复
select * from b where yhid in(select yhid from b union select yhid from c)
union
select * from c where yhid in(select yhid from b union select yhid from c)
DEATH64 2010-10-07
  • 打赏
  • 举报
回复
表B和表C同时有,取那个?
phyedu 2010-10-07
  • 打赏
  • 举报
回复
intersect是Analysis Services,而不是Transact-SQL,不能作为SQL语句使用??
phyedu 2010-10-07
  • 打赏
  • 举报
回复
有错误 intersect?
999朵玫瑰 2010-10-07
  • 打赏
  • 举报
回复
学习下
zhaojianmi1 2010-10-07
  • 打赏
  • 举报
回复
select * from b where yhid in ((select yhid from b) intersect (select yhid from c))
union
select * from c where yhid in ((select yhid from b) intersect (select yhid from c))
zhaojianmi1 2010-10-07
  • 打赏
  • 举报
回复
先坐下

34,590

社区成员

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

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