其中这五张表的表结果基本上相同,前三个字段相同,组合起来是表中的唯一索引。不同的只是最后一个字段不一样。
比如A表的数据是:
OMP_ID BTS_ID CELL_ID TCHTRAFFICS
3826 96 Jan-96 0.411111111
3826 96 Feb-96 3.586111111
3826 96 Mar-96 0.744444444
3826 278 278-1 0.522222222
3826 278 278-2 0.444444444
3826 278 278-3 2.85
3826 476 476-1 0.372222222
3826 476 476-2 0.172222222
3826 476 476-3 0.088888889
3826 478 478-1 4.594444444
3826 478 478-2 0.433333333
3826 478 478-3 18.53333333
3826 479 479-1 2.294444444
3826 479 479-2 1.088888889
3826 479 479-3 2.444444444
3826 554 554-1 0.197222222
B,C,D,E四个表的数据根上面A表的数据一样,只是最后一个字段不同。
请问如何才能将以上五个表的数据连接起来,放到同一张表中,效果是:
OMP_ID BTS_ID CELL_ID TCHTRAFFICS B表最后字段 C表最后字段 D表最后字段 E表最后字段
12 个解决方案
#1
全链接把
select a.OMP_ID,a.BTS_ID,a.CELL_ID,a.TCHTRAFFICS,b.TCHTRAFFICS,c.TCHTRAFFICS,d.TCHTRAFFICS,e.TCHTRAFFICS
from a,b,c,d,e
where a.OMP_ID = b.OMP_ID
and a.BTS_ID = b.BTS_ID
and a.CELL_ID = b.CELL_ID
.
.
.
select a.OMP_ID,a.BTS_ID,a.CELL_ID,a.TCHTRAFFICS,b.TCHTRAFFICS,c.TCHTRAFFICS,d.TCHTRAFFICS,e.TCHTRAFFICS
from a,b,c,d,e
where a.OMP_ID = b.OMP_ID
and a.BTS_ID = b.BTS_ID
and a.CELL_ID = b.CELL_ID
.
.
.
#2
select omp_id,bts_id,cell_id,A_endcol,null,null,null,null from A
union all
select omp_id,bts_id,cell_id,null,B_endcol,null,null,null from B
union all
select omp_id,bts_id,cell_id,null,null,C_endcol,null,null from C
union all
select omp_id,bts_id,cell_id,null,null,null,D_endcol,null from D
union all
select omp_id,bts_id,cell_id,null,null,null,null,E_endcol from E
#3
A,B,C,D,E表中OMP_ID BTS_ID CELL_ID的3列完全相同的情况下,是往表里面插入:一条数据还是五条数据?
#4
select a.OMP_ID,a.BTS_ID,a.CELL_ID,a.TCHTRAFFICS,
b.TCHTRAFFICS,c.TCHTRAFFICS,d.TCHTRAFFICS,e.TCHTRAFFICS
from a,b,c,d,e
where a.omp_id = b.omp_id and a.omp_id = c.omp_id and a.omp_id = d.om and a.omp_id = e.omp_id
and a.bts_id = b.bts_id and a.bts_id = c.tbs_id and a.tbs_id = d.bts_id and a.bts_id = e.bts_id
and a.child_id = b.child_id and a.child_id = c.child_id and a.child_id = d.child_id
and a.child_id = e.child_id
b.TCHTRAFFICS,c.TCHTRAFFICS,d.TCHTRAFFICS,e.TCHTRAFFICS
from a,b,c,d,e
where a.omp_id = b.omp_id and a.omp_id = c.omp_id and a.omp_id = d.om and a.omp_id = e.omp_id
and a.bts_id = b.bts_id and a.bts_id = c.tbs_id and a.tbs_id = d.bts_id and a.bts_id = e.bts_id
and a.child_id = b.child_id and a.child_id = c.child_id and a.child_id = d.child_id
and a.child_id = e.child_id
#5
select a.OMP_ID,a.BTS_ID,a.CELL_ID,a.TCHTRAFFICS,b.TCHTRAFFICS,c.TCHTRAFFICS,
d.TCHTRAFFICS,e.TCHTRAFFICS
from a,b,c,d,e
where a.TCHTRAFFICS=b.TCHTRAFFICS and a.TCHTRAFFICS=c.TCHTRAFFICS and a.TCHTRAFFICS=c.TCHTRAFFICS and a.TCHTRAFFICS=d.TCHTRAFFICS and a.TCHTRAFFICS=e.TCHTRAFFICS and ....后面的就自己写吧
#6
强烈支持!
#7
select omp_id,bts_id,cell_id,A_endcol,null,null,null,null from A
union
select omp_id,bts_id,cell_id,null,B_endcol,null,null,null from B
union
select omp_id,bts_id,cell_id,null,null,C_endcol,null,null from C
union
select omp_id,bts_id,cell_id,null,null,null,D_endcol,null from D
union
select omp_id,bts_id,cell_id,null,null,null,null,E_endcol from E
union all 会有重复记录可能, 还是用union好。
#8
在外层在加个group by
#9
学习了一下!
#10
4、5楼的语句我不明白,为什么要查出三个字段都相等的数据呢,楼主要的是所有的数据。如果
a表中有:1、2、3、4
b表中有:2、3、4、5
c表中有:3、4、5、6
D表中有 4、5、6、7
E表中有:5、6、7、8 那这几条数据是不是就被丢弃了?
如果5个表中存在完全相同的记录,那么2楼的语句是不是也不成立了。新表中不应该允许重复记录出现吧?
a表中有:1、2、3、4
b表中有:2、3、4、5
c表中有:3、4、5、6
D表中有 4、5、6、7
E表中有:5、6、7、8 那这几条数据是不是就被丢弃了?
如果5个表中存在完全相同的记录,那么2楼的语句是不是也不成立了。新表中不应该允许重复记录出现吧?
#11
select omp_id,bts_id,cell_id,A_endcol,null B_endcol,null C_endcol,null D_endcol,
null E_endcol from A
union all
select omp_id,bts_id,cell_id,null,B_endcol,null,null,null from B
union all
select omp_id,bts_id,cell_id,null,null,C_endcol,null,null from C
union all
select omp_id,bts_id,cell_id,null,null,null,D_endcol,null from D
union all
select omp_id,bts_id,cell_id,null,null,null,null,E_endcol from E
null E_endcol from A
union all
select omp_id,bts_id,cell_id,null,B_endcol,null,null,null from B
union all
select omp_id,bts_id,cell_id,null,null,C_endcol,null,null from C
union all
select omp_id,bts_id,cell_id,null,null,null,D_endcol,null from D
union all
select omp_id,bts_id,cell_id,null,null,null,null,E_endcol from E
#12
晕倒,分几步去做。
insert 新表(前三字段)
SELECT distinct 前三字段 from(
select 前三字段 from 表A
UNION ALL
select 前三字段 from 表B
UNION ALL
select 前三字段 from 表C
UNION ALL
select 前三字段 from 表D
UNION ALL
select 前三字段 from 表E
UNION ALL );
后面去update就是了
insert 新表(前三字段)
SELECT distinct 前三字段 from(
select 前三字段 from 表A
UNION ALL
select 前三字段 from 表B
UNION ALL
select 前三字段 from 表C
UNION ALL
select 前三字段 from 表D
UNION ALL
select 前三字段 from 表E
UNION ALL );
后面去update就是了
#1
全链接把
select a.OMP_ID,a.BTS_ID,a.CELL_ID,a.TCHTRAFFICS,b.TCHTRAFFICS,c.TCHTRAFFICS,d.TCHTRAFFICS,e.TCHTRAFFICS
from a,b,c,d,e
where a.OMP_ID = b.OMP_ID
and a.BTS_ID = b.BTS_ID
and a.CELL_ID = b.CELL_ID
.
.
.
select a.OMP_ID,a.BTS_ID,a.CELL_ID,a.TCHTRAFFICS,b.TCHTRAFFICS,c.TCHTRAFFICS,d.TCHTRAFFICS,e.TCHTRAFFICS
from a,b,c,d,e
where a.OMP_ID = b.OMP_ID
and a.BTS_ID = b.BTS_ID
and a.CELL_ID = b.CELL_ID
.
.
.
#2
select omp_id,bts_id,cell_id,A_endcol,null,null,null,null from A
union all
select omp_id,bts_id,cell_id,null,B_endcol,null,null,null from B
union all
select omp_id,bts_id,cell_id,null,null,C_endcol,null,null from C
union all
select omp_id,bts_id,cell_id,null,null,null,D_endcol,null from D
union all
select omp_id,bts_id,cell_id,null,null,null,null,E_endcol from E
#3
A,B,C,D,E表中OMP_ID BTS_ID CELL_ID的3列完全相同的情况下,是往表里面插入:一条数据还是五条数据?
#4
select a.OMP_ID,a.BTS_ID,a.CELL_ID,a.TCHTRAFFICS,
b.TCHTRAFFICS,c.TCHTRAFFICS,d.TCHTRAFFICS,e.TCHTRAFFICS
from a,b,c,d,e
where a.omp_id = b.omp_id and a.omp_id = c.omp_id and a.omp_id = d.om and a.omp_id = e.omp_id
and a.bts_id = b.bts_id and a.bts_id = c.tbs_id and a.tbs_id = d.bts_id and a.bts_id = e.bts_id
and a.child_id = b.child_id and a.child_id = c.child_id and a.child_id = d.child_id
and a.child_id = e.child_id
b.TCHTRAFFICS,c.TCHTRAFFICS,d.TCHTRAFFICS,e.TCHTRAFFICS
from a,b,c,d,e
where a.omp_id = b.omp_id and a.omp_id = c.omp_id and a.omp_id = d.om and a.omp_id = e.omp_id
and a.bts_id = b.bts_id and a.bts_id = c.tbs_id and a.tbs_id = d.bts_id and a.bts_id = e.bts_id
and a.child_id = b.child_id and a.child_id = c.child_id and a.child_id = d.child_id
and a.child_id = e.child_id
#5
select a.OMP_ID,a.BTS_ID,a.CELL_ID,a.TCHTRAFFICS,b.TCHTRAFFICS,c.TCHTRAFFICS,
d.TCHTRAFFICS,e.TCHTRAFFICS
from a,b,c,d,e
where a.TCHTRAFFICS=b.TCHTRAFFICS and a.TCHTRAFFICS=c.TCHTRAFFICS and a.TCHTRAFFICS=c.TCHTRAFFICS and a.TCHTRAFFICS=d.TCHTRAFFICS and a.TCHTRAFFICS=e.TCHTRAFFICS and ....后面的就自己写吧
#6
强烈支持!
#7
select omp_id,bts_id,cell_id,A_endcol,null,null,null,null from A
union
select omp_id,bts_id,cell_id,null,B_endcol,null,null,null from B
union
select omp_id,bts_id,cell_id,null,null,C_endcol,null,null from C
union
select omp_id,bts_id,cell_id,null,null,null,D_endcol,null from D
union
select omp_id,bts_id,cell_id,null,null,null,null,E_endcol from E
union all 会有重复记录可能, 还是用union好。
#8
在外层在加个group by
#9
学习了一下!
#10
4、5楼的语句我不明白,为什么要查出三个字段都相等的数据呢,楼主要的是所有的数据。如果
a表中有:1、2、3、4
b表中有:2、3、4、5
c表中有:3、4、5、6
D表中有 4、5、6、7
E表中有:5、6、7、8 那这几条数据是不是就被丢弃了?
如果5个表中存在完全相同的记录,那么2楼的语句是不是也不成立了。新表中不应该允许重复记录出现吧?
a表中有:1、2、3、4
b表中有:2、3、4、5
c表中有:3、4、5、6
D表中有 4、5、6、7
E表中有:5、6、7、8 那这几条数据是不是就被丢弃了?
如果5个表中存在完全相同的记录,那么2楼的语句是不是也不成立了。新表中不应该允许重复记录出现吧?
#11
select omp_id,bts_id,cell_id,A_endcol,null B_endcol,null C_endcol,null D_endcol,
null E_endcol from A
union all
select omp_id,bts_id,cell_id,null,B_endcol,null,null,null from B
union all
select omp_id,bts_id,cell_id,null,null,C_endcol,null,null from C
union all
select omp_id,bts_id,cell_id,null,null,null,D_endcol,null from D
union all
select omp_id,bts_id,cell_id,null,null,null,null,E_endcol from E
null E_endcol from A
union all
select omp_id,bts_id,cell_id,null,B_endcol,null,null,null from B
union all
select omp_id,bts_id,cell_id,null,null,C_endcol,null,null from C
union all
select omp_id,bts_id,cell_id,null,null,null,D_endcol,null from D
union all
select omp_id,bts_id,cell_id,null,null,null,null,E_endcol from E
#12
晕倒,分几步去做。
insert 新表(前三字段)
SELECT distinct 前三字段 from(
select 前三字段 from 表A
UNION ALL
select 前三字段 from 表B
UNION ALL
select 前三字段 from 表C
UNION ALL
select 前三字段 from 表D
UNION ALL
select 前三字段 from 表E
UNION ALL );
后面去update就是了
insert 新表(前三字段)
SELECT distinct 前三字段 from(
select 前三字段 from 表A
UNION ALL
select 前三字段 from 表B
UNION ALL
select 前三字段 from 表C
UNION ALL
select 前三字段 from 表D
UNION ALL
select 前三字段 from 表E
UNION ALL );
后面去update就是了