多有联合查询

小草旁的大树 2010-09-11 09:46:13
表A
a_id b_id c_id
1 1 1
2 null 1
3 1 null
4 null null

表B
b_id type1
1 BB

表C
c_id type2
1 CC

查询结果显示:
1 BB CC
2 null CC
3 BB null
4 null null
...全文
61 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
njlywy 2010-09-11
  • 打赏
  • 举报
回复
select a.a_id,b.type1,c.type2
from a left join b on a.b_id = b.b_id
left join c on a.c_id = c.c_id
drop table a , b, c
sweetbbs 2010-09-11
  • 打赏
  • 举报
回复
select a_id,type1,type2 from a
left join b on a.b_id=b.b_id left join c on a.c_id=c.c_id
dawugui 2010-09-11
  • 打赏
  • 举报
回复
create table A(a_id int,b_id int,c_id int)
insert into a values(1 ,1 ,1)
insert into a values(2 ,null ,1)
insert into a values(3 ,1 ,null)
insert into a values(4 ,null ,null)
create table B(b_id int,type1 varchar(10))
insert into b values(1 ,'BB')
create table C(c_id int,type2 varchar(10))
insert into c values( 1 ,'CC')
go

--方法一
select a.a_id,
type1 = (select b.type1 from b where b.b_id = a.b_id),
type2 = (select c.type2 from c where c.c_id = a.c_id)
from a

--方法二
select a.a_id,
b.type1,
c.type2
from a left join b on a.b_id = b.b_id
left join c on a.c_id = c.c_id
drop table a , b, c

/*
a_id type1 type2
----------- ---------- ----------
1 BB CC
2 NULL CC
3 BB NULL
4 NULL NULL

(所影响的行数为 4 行)

*/
dawugui 2010-09-11
  • 打赏
  • 举报
回复
select a.a_id,
(select b.type1 from b where b.b_id = a.b_id),
(select c.type1 from c where c.c_id = a.c_id)
from a

22,210

社区成员

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

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