求助:经典SQL面试验题,在线等答案 急!!!

woshipizi 2007-10-05 11:25:41
/*
题目1:
有cc、dd两张表A、B是组合主键
cc表
A B C
10 11 33
13 15 NULL
dd表
A B D
10 11 34
20 30 NULL
怎么才能查出如下结果:
A B C D
10 11 33 34
13 15 NULL NULL
20 30 NULL NULL
要求:写出T-SQL语句,不能通过存储过程等!
select a,b,c,d
from cc left join dd
on cc.a=dd.a
用上面的语句显示错误,不能确定a和b 是哪个表的
如果变成
select cc.a,cc.b,c,d
from cc left join dd
on cc.a=dd.a
只能显示cc表中的两条
请高手帮忙解决
...全文
247 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshipizi 2007-10-11
  • 打赏
  • 举报
回复
我没有太多得分大家分给的少大家都有
辛苦了
woshipizi 2007-10-11
  • 打赏
  • 举报
回复
非常感谢大家的指教
yingzi5839 2007-10-10
  • 打赏
  • 举报
回复
2楼的就对
dawugui 2007-10-10
  • 打赏
  • 举报
回复
create table cc(A int, B int, C int)
insert into cc values(10, 11, 33 )
insert into cc values(13, 15, NULL)
create table dd(A int, B int, D int)
insert into dd values(10, 11, 34 )
insert into dd values(20, 30, NULL)
go

select isnull(cc.A,dd.A) A, isnull(cc.B,dd.B) B,cc.C,dd.D
from cc full join dd on cc.A = dd.A and cc.B = dd.B

--drop table cc,dd

/*
A B C D
----------- ----------- ----------- -----------
10 11 33 34
13 15 NULL NULL
20 30 NULL NULL

(所影响的行数为 3 行)
*/
dawugui 2007-10-10
  • 打赏
  • 举报
回复
select isnull(cc.A,dd.A) A, isnull(cc.B,dd.B) B,cc.C,dd.D
from cc full join dd on cc.A = dd.A and cc.B = dd.B
xiaoku 2007-10-10
  • 打赏
  • 举报
回复
路过接分...
cs_oldhorse 2007-10-10
  • 打赏
  • 举报
回复
select #tmpCC.A,#tmpCC.B,#tmpCC.C,#tmpDD.D
from #tmpCC left join #tmpDD
on #tmpCC.A=#tmpDD.A and #tmpCC.B=#tmpDD.B
union
select #tmpDD.A,#tmpDD.B,#tmpCC.C,#tmpDD.D
from #tmpDD left join #tmpCC
on #tmpDD.A=#tmpCC.A and #tmpDD.B=#tmpCC.B

2个外连接测试通过
yooooyiiiiiiiiii 2007-10-10
  • 打赏
  • 举报
回复
select s.a,s.b,s.c,t.d from A s left outer join B t
on s.a=t.a and s.b=t.b
union
select tt.a,tt.b,ss.c,tt.d from A ss right outer join B tt
on ss.a=tt.a and ss.b=tt.b

这个可以 用2个外连接就可以得到了.我测试通过了.
yooooyiiiiiiiiii 2007-10-05
  • 打赏
  • 举报
回复
SELECT A.a,A.b,A.c,B.d FROM cc A FULL OUTER JOIN dd B ON A.a=B.a and A.b=B.b
不知道对不对。
OracleRoob 2007-10-05
  • 打赏
  • 举报
回复
--应该这样处理:



create table #CC(A int, B int, C int)

insert into #CC select 10, 11, 33
insert into #CC select 13, 15, NULL

create table #DD(A int, B int, D int)

insert into #DD select 10, 11, 34
insert into #DD select 20, 30, NULL


select * from #CC
select * from #DD


select t.A,t.B,t1.C,t2.D
from
(
select A,B from #CC
union
select A,B from #DD
) as t
left join #CC as t1 on t.A=t1.A and t.B=t1.B
left join #DD as t2 on t.A=t2.A and t.B=t2.B

drop table #CC,#DD
mustudent 2007-10-05
  • 打赏
  • 举报
回复
create table #cc
(
a char(2)
,b char(2)
,c char(2)
)
create table #dd
(
a char(2)
,b char(2)
,d char(2)
)

insert #cc
select '10','11','33'
union all
select '13','15',null

insert #dd
select '10','11','34'
union all
select '20','30',null

select * From #dd

select * From #cc
select isnull(CC.A,DD.A),IsNull(CC.B,DD.B),CC.C,DD.D
from #CC cc
full outer join #DD dd on CC.A=DD.A and CC.B=DD.B

drop table #cc
drop table #dd
OracleRoob 2007-10-05
  • 打赏
  • 举报
回复
--这样?

select CC.A,CC.B,CC.C,DD.D
from CC
left join DD on CC.A=DD.A and CC.B=DD.B
zanyzyg 2007-10-05
  • 打赏
  • 举报
回复

对啊,就是个全连接吗!
playwarcraft 2007-10-05
  • 打赏
  • 举报
回复
--full join就可以了

create table #CC(A int, B int, C int)

insert into #CC select 10, 11, 33
insert into #CC select 13, 15, NULL

create table #DD(A int, B int, D int)

insert into #DD select 10, 11, 34
insert into #DD select 20, 30, NULL


select isnull(#cc.a,#dd.a) as A ,isnull(#cc.b,#dd.b) as B,#cc.c,#dd.d
from #cc
full join #dd
on #cc.a=#dd.a and #cc.b=#dd.b

/*
A B c d
----------- ----------- ----------- -----------
10 11 33 34
13 15 NULL NULL
20 30 NULL NULL
*/
drop table #cc,#dd
简若的学习窝 2007-10-05
  • 打赏
  • 举报
回复
select X.a, X.b,X.c,Y.d
from X left join Y
on X.a=Y.a and X.b=Y.b
union
select Y.a, Y.b,X.c,Y.d
from Y left join X
on X.a=Y.a and X.b=Y.b

22,209

社区成员

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

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