SQL联合查询问题

来源:百度知道 编辑:UC知道 时间:2024/05/06 15:10:36
假如我有表table1,与table2
其中个表的字段大多相同,我取出他们相同的字段,放在一个表中显示,如
table1
字段1 字段2 字段3 字段4 字段5
1 2 3 4 5
table2
字段1 字段2 字段3 字段4 字段6
4 5 6 7 8
我要的结果是
字段1 字段2 字段3 字段4
1 2 3 4
4 5 6 7

我要速度啊,急的

设s表有字段a,b,c,d.还有m表有字段d,e,f,g
可以这么写
select s.a, s.b, s.c, s.d, m.e, m.f, m.g
from s,m
where s.d = m.d

select '字段1','字段2','字段3','字段4' from table1
union all
select '字段1','字段2','字段3','字段4' from table2

看测试数据:
create table #tbl_a(seg1 int,seg2 int,seg3 int,seg4 int,seg5 int)
create table #tbl_b(seg1 int,seg2 int,seg3 int,seg4 int,seg6 int)
insert into #tbl_a values(1,2,3,4,5)
insert into #tbl_b values(6,7,8,9,10)

select seg1,seg2,seg3,seg4 from #tbl_a
union all
select seg1,seg2,seg3,seg4 from #tbl_b

结果:
seg1 seg2 seg3 seg4
----------- ----------- ----------- -----------
1 2 3 4
6 7 8 9

(所影响的行数为 2 行)

用视图关联起来就可以!