求助!求助!求助!求SQL语句
表1为头表,数据如下:
0010 A001 10 20070430
0020 A002 20 20070430
表2为子表,数据如下:
0010 A001 a1 5 20070430
0010 A001 b1 2 20070430
0010 A001 c1 3 20070430
0020 A002 a2 15 20070430
0020 A002 b2 5 20070430
问高手如何用select语句检索数据,如以下效果
0010 A001 10 20070430
A001 5 a1 20070430
A001 2 b1 20070430
A001 3 c1 20070430
0020 A002 20 20070430
A002 15 a2 20070430
A002 5 b2 20070430
------解决方案--------------------declare @a table(a varchar(6),b varchar(6),c varchar(10) ,d varchar(10))
insert @a select '0010 ', 'A001 ', 10 , '20070430 '
union all select '0020 ' , 'A002 ', 20 , '20070430 '
declare @b table(a varchar(6),b varchar(6),c varchar(10),d varchar(10),e varchar(10))
insert @b select '0010 ', 'A001 ', 'a1 ', 5 , '20070430 '
union all select '0010 ', 'A001 ', 'b1 ', 2 , '20070430 '
union all select '0010 ', 'A001 ', 'c1 ', 3 , '20070430 '
union all select '0020 ', 'A002 ', 'a2 ', 15 , '20070430 '
union all select '0020 ', 'A002 ', 'b2 ', 5 , '20070430 '
select a,b,c,d from(
select *,e=a from @a
union all
select b,d,c,e,a from @b )
dd
order by e,a
------解决方案--------------------select a,b,c,a1,d from(
select a,b,c, ' ' as a1,d from @a
union all
select ' ' as bcd,b,d,c,e from @b )
dd