如何写SQL?
表A
ID Name
1 壹
2 贰
3 叁
4 肆
5 伍
6 陆
7 七
8 捌
9 九
表B
ID Bname
1 Ⅰ
2 Ⅱ
4 Ⅳ
5 Ⅴ
6 Ⅵ 表C
ID Cname
3 NULL
7 Ⅶ
8 Ⅷ
9 Ⅸ 用SQl写,结果如下:
1 壹 Ⅰ
2 贰 Ⅱ
3 叁 NULL
4 肆 Ⅳ
5 伍 Ⅴ
6 陆 Ⅵ
7 七 Ⅶ
8 捌 Ⅷ
9 九 Ⅸ
表A
ID Name
1 壹
2 贰
3 叁
4 肆
5 伍
6 陆
7 七
8 捌
9 九
表B
ID Bname
1 Ⅰ
2 Ⅱ
4 Ⅳ
5 Ⅴ
6 Ⅵ 表C
ID Cname
3 NULL
7 Ⅶ
8 Ⅷ
9 Ⅸ 用SQl写,结果如下:
1 壹 Ⅰ
2 贰 Ⅱ
3 叁 NULL
4 肆 Ⅳ
5 伍 Ⅴ
6 陆 Ⅵ
7 七 Ⅶ
8 捌 Ⅷ
9 九 Ⅸ
select a.id,a.name,b.Bname from a left join b on a.id =b .id
union all
select a.id,a.name,b.Cname from a left join c on c.id =a.id)
d order by id
declare @tB table (ID int,cName nvarchar(20))
declare @tC table (ID int,cName nvarchar(20))
insert into @tA select 1,'一'
insert into @tA select 2,'二'
insert into @tA select 3,'三'
insert into @tA select 4,'四'
insert into @tA select 5,'五'
insert into @tA select 6,'六'
insert into @tA select 7,'七'
insert into @tA select 8,'八'
insert into @tA select 9,'九'
insert into @tB select 1,'Ⅰ'
insert into @tB select 2,'Ⅱ'
insert into @tB select 4,'Ⅳ'
insert into @tB select 5,'Ⅴ'
insert into @tB select 6,'Ⅵ'
insert into @tC select 3,null
insert into @tC select 7,'Ⅶ'
insert into @tC select 8,'Ⅷ'
insert into @tC select 9,'Ⅸ'
select * from @tA
select * from @tB
select * from @tCselect A.ID,A.cName,E.cName
from @tA A
left join (select ID,cName from @tB
union all
select ID,cName from @tC) as E
on (A.ID=E.ID)
declare @tB table (ID int,bName nvarchar(20))
declare @tC table (ID int,cName nvarchar(20))
insert @tA select 1,'壹'union all
select 2,'贰'union all
select 3,'叁'union all
select 4,'肆'union all
select 5,'伍'union all
select 6,'陆'union all
select 7,'七'union all
select 8,'捌'union all
select 9,'九'
insert into @tB select 1,'Ⅰ'union all
select 2,'Ⅱ'union all
select 4,'Ⅳ'union all
select 5,'Ⅴ'union all
select 6,'Ⅵ'
insert into @tC select 3,null union all
select 7,'Ⅶ' union all
select 8,'Ⅷ' union all
select 9,'Ⅸ'select aname,bname From @ta a left join (select *from @tb union all select *From @tc) b on a.id=b.id
同Visual Studio 2005以及Microsoft .NET集成的开发特性,通过实例由浅入深的帮助您掌握这些看似难懂的技术。
http://auction1.taobao.com/auction/item_detail-0db2-512881e1a3aab7111ccf190b0068536d.jhtml