sql 语句,如何将数个相同表中的数据合并到一张表中
我有这样三张或者更多的表,格式为table1valuetimea12012-7-3101:00:00a32012-7-3103:00:00a42012-7-3104:00...
我有这样三张或者更多的表,格式为
table1
value time
a1 2012-7-31 01:00:00
a3 2012-7-31 03:00:00
a4 2012-7-31 04:00:00
a5 2012-7-31 05:00:00
table2
value time
b1 2012-7-31 01:00:00
b2 2012-7-31 02:00:00
b4 2012-7-31 04:00:00
b5 2012-7-31 05:00:00
table3
value time
c1 2012-7-31 01:00:00
c2 2012-7-31 02:00:00
c3 2012-7-31 03:00:00
c5 2012-7-31 05:00:00
我要怎么写sql语句能得到如下结果?
value1 value2 value3 time
a1 b1 c1 2012-7-31 01:00:00
NULL b2 c2 2012-7-31 02:00:00
a3 NULL c3 2012-7-31 03:00:00
a4 b4 NULL 2012-7-31 04:00:00
a5 b5 c5 2012-7-31 05:00:00
是用full join再合并吗??? 展开
table1
value time
a1 2012-7-31 01:00:00
a3 2012-7-31 03:00:00
a4 2012-7-31 04:00:00
a5 2012-7-31 05:00:00
table2
value time
b1 2012-7-31 01:00:00
b2 2012-7-31 02:00:00
b4 2012-7-31 04:00:00
b5 2012-7-31 05:00:00
table3
value time
c1 2012-7-31 01:00:00
c2 2012-7-31 02:00:00
c3 2012-7-31 03:00:00
c5 2012-7-31 05:00:00
我要怎么写sql语句能得到如下结果?
value1 value2 value3 time
a1 b1 c1 2012-7-31 01:00:00
NULL b2 c2 2012-7-31 02:00:00
a3 NULL c3 2012-7-31 03:00:00
a4 b4 NULL 2012-7-31 04:00:00
a5 b5 c5 2012-7-31 05:00:00
是用full join再合并吗??? 展开
展开全部
这个问题首先必须基于table1,table2,table3这3张表各自的time字段无重复值,否则每个有重复的时间,都可能会组合成N多种组合(假如这3张表每一张都有两条时间为2012-07-31 01:00:00的记录,那么单单就“2012-07-31 01:00:00”这个时间就可以组合成2x2x2=8中组合,重复愈多出现的组合越多,大的重复记录,因互相乘积的原因,很容易会导致出现天文数字种组合。它一方面会导致效率低下,另一方面还会出现在这些组合中到底取舍那条记录的问题,有时候单凭SQL是很难解决。因此如要实现楼主的要求,应确保各自表中无重复时间记录,否则合并结果将难令人满意,楼主可以测试各种情况和代码以证实的本人说法。
先用UNION操作符求出3张表唯一的时间列表,该子查询表取别名t,然后分别用 t 与其它3张表基于time字段进行left join,最后求出最终结果集。
为了便于理解该SQL语句,我对那3张表未使用表别名,代码字符数看起来会比较多。
select table1.[value] as value1,table2.[value] as value2,table3.[value] as value3,t.[time] from
(((select [time] from table1 union select [time] from table2 union select [time] from table3) t
left join table1 on
t.[time]=table1.[time])
left join table2 on
t.[time]=table2.[time])
left join table3 on
t.[time]=table3.[time]
order by t.[time]
注:基于单独表内无重复时间记录。代码已测试通过,如果楼主有超过3张以上相同的表,照套上面写法即可,
如果4张以上的表相关SQL代码不知如何写,可以向我发追问。
先用UNION操作符求出3张表唯一的时间列表,该子查询表取别名t,然后分别用 t 与其它3张表基于time字段进行left join,最后求出最终结果集。
为了便于理解该SQL语句,我对那3张表未使用表别名,代码字符数看起来会比较多。
select table1.[value] as value1,table2.[value] as value2,table3.[value] as value3,t.[time] from
(((select [time] from table1 union select [time] from table2 union select [time] from table3) t
left join table1 on
t.[time]=table1.[time])
left join table2 on
t.[time]=table2.[time])
left join table3 on
t.[time]=table3.[time]
order by t.[time]
注:基于单独表内无重复时间记录。代码已测试通过,如果楼主有超过3张以上相同的表,照套上面写法即可,
如果4张以上的表相关SQL代码不知如何写,可以向我发追问。
展开全部
楼上的回答有点问题,time会有重复的情况
我的解决方案是先求出时间表 tt,然后用tt和其他表外连接。
select t1.value value1, t2.value value2, t3.value value3, tt.time from (
select distinct nvl(nvl(t1.time,t2.time),t3.time) time
from table1 t1 full join table2 t2 on t1.time=t2.time full join table3 t3 on t1.time=t3.time) tt
full join table1 t1 on tt.time = t1.time full join table2 t2 on tt.time=t2.time full join table3 t3 on tt.time = t3.time
我的解决方案是先求出时间表 tt,然后用tt和其他表外连接。
select t1.value value1, t2.value value2, t3.value value3, tt.time from (
select distinct nvl(nvl(t1.time,t2.time),t3.time) time
from table1 t1 full join table2 t2 on t1.time=t2.time full join table3 t3 on t1.time=t3.time) tt
full join table1 t1 on tt.time = t1.time full join table2 t2 on tt.time=t2.time full join table3 t3 on tt.time = t3.time
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select t1.value,t2.value,t3.value,timeTab.time from (select time from t1
UNION
select time from t2
UNION
select time from t3) timeTab
LEFT JOIN t1 ON timeTab.time=t1.time
LEFT JOIN t2 ON timeTab.time=t2.time
LEFT JOIN t3 ON timeTab.time=t3.time
order by timeTab.time
UNION
select time from t2
UNION
select time from t3) timeTab
LEFT JOIN t1 ON timeTab.time=t1.time
LEFT JOIN t2 ON timeTab.time=t2.time
LEFT JOIN t3 ON timeTab.time=t3.time
order by timeTab.time
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
insert into TargetTable
select * from table1
union
select * from table2
union
......
select * from table1
union
select * from table2
union
......
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select a.[value] as value1,b.[value] as value2, c.[value] as value3,
isnull(isnull(a.time,b.time),c.time) as time
from table1 a full join table2 b on a.time = b.time
full join table3 c on a.time = c.time
isnull(isnull(a.time,b.time),c.time) as time
from table1 a full join table2 b on a.time = b.time
full join table3 c on a.time = c.time
追问
谢谢回答,不过有点问题,结果里面会有两条time为 2012-7-31 02:00:00 的值:
NULL b2 NULL
NULL NULL c2
这两条没有合并起来,还有就是如果有3个以上这样的表,该怎么写啊,谢谢!
追答
那检查一下time的数据类型是什么呢?
如果两条,说明table2里的和table3里的这个值是不相等的
如果有更多表的话,from。。。 这里,就是加full join 就可以了
但是select 里的 time列值选取,要注意一下
不过也简单,就是套 isnull ,比如多一个table4
select a.[value] as value1,b.[value] as value2, c.[value] as value3,d.[value] as value4,
isnull(isnull(isnull(a.time,b.time),c.time),d.time) as time
from table1 a full join table2 b on a.time = b.time
full join table3 c on a.time = c.time
full join table4 d on a.time = d.time
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询