求SQL汇总语句..请帮忙
表1
id name
1 a
2 b
3 c
表2
idmc name2 name3
1 a1 hg
1 a2 kj
1 a3 fd
1 a4 tr
2 b1 uy
2 b2 s
3 c4 jk
3 c6 ty
要得到的结果:
1 a
1 a1 hg
1 a2 kj
1 a3 fd
1 a4 tr
2 b
2 b1 uy
2 b2 s
3 c
3 c4 jk
3 c6 ty
请帮忙
[解决办法]
Select * From
(Select id,name as 'Name2 ', ' ' as 'Name3 ' from 表1
union all
Select idmc as 'id ',name2,name3 from 表2) a
order by a.id
[解决办法]
--如果name列是可以排序的话
select id,name, ' ' as name2 from table1
union
select idmc,name2,name3 from table2
order by name
--如果不可以排序
select id,name,name2 from
(
select id,name, ' ' as name2,0 as sort from table1
union
select idmc,name2,name3,1 from table2
)tt
order by id,sort
[解决办法]
select id,Name,Name2,Name3 ' from
(
Select id,name as Name, ' ' as Name2, ' ' as Name3,0 as sort from 表1
union all
Select idmc as id, ' ' as NAME,name2,name3,1 as sort from 表2
) t
order by id,sort,Name2
[解决办法]
select * from
( select id,name, ' ' from table1
union all
select idmc,name2,name3 from table2
) a
order by id