日期:2014-05-17  浏览次数:20755 次

100 分 求一sql语句
两张表 A, B 字段一样如下

 f1 f2 f3 f4 time




A 表数据

 f1 f2 f3 f4 time
 1 2 12:00
 3 4 13:00

B 表数据

 f1 f2 f3 f4 time
  12 13 12:00
  15 16

得到查询结果如下:
 f1 f2 f3 f4 time
 1 2 12 13 12:00
 3 15 4 16 13:00

相当于合并两个表,空数据的地方取另一个表的数据来填充。

我用的是orcal数据库。真心求一sql语句,非常感谢。

分不够我可以再开贴加




------解决方案--------------------
A表和B表的数据与字段的对应有点混乱
------解决方案--------------------
问题是你 找到 B 的哪条记录 去填

如果 B 中还有数据为

 
XML code

  f1 f2 f3 f4 time 
        20 21 12:00

------解决方案--------------------
是不是还少个主要字段啊 2个如何关联起来呢?
------解决方案--------------------
-- 一个重要的问题没搞明白:A表和B表是怎么关联记录行滴?
------解决方案--------------------
根据time来关联?

SQL code

select a.f1||b.f1,a.f2||b.f2,a.f3||b.f3,a.f4||b.f4,a.time
from A,B
where a.time=b.time

------解决方案--------------------
如果一个时间都是一个空格对呀一个数字的话 这样应该可以了

SQL code

select a.f1||b.f1 f1,a.f2||b.f2 f2 ,a.f3||b.f3 f3 ,a.f4||b.f4 f4,a.time
from A,B
where a.time=b.time

------解决方案--------------------
SQL code

select nvl(a.f1, b.f1) f1 ,nvl(a.f2,b.f2) f2 ,nvl(a.f3,b.f3) f3 ,nvl(a.f4,b.f4) f4 from testA a , testB b where a.time=b.time ;

------解决方案--------------------
SQL code

create table taba
(f1 int, f2 int, f3 int, f4 int, times varchar(7))

insert into taba
select 1,2,null,null,'12:00' union all
select 3,null,4,null,'13:00'

create table tabb
(f1 int, f2 int, f3 int, f4 int, times varchar(7))

insert into tabb
select null,null,12,13,'12:00' union all
select null,15,null,16,'13:00'


select 
coalesce(a.f1,b.f1) f1,
coalesce(a.f2,b.f2) f2,
coalesce(a.f3,b.f3) f3,
coalesce(a.f4,b.f4) f4,
a.times
from taba a
inner join tabb b
on a.times=b.times

f1          f2          f3          f4          times
----------- ----------- ----------- ----------- -------
1           2           12          13          12:00
3           15          4           16          13:00

(2 row(s) affected)

------解决方案--------------------
SQL code

select sum(nvl(f1,0)) f1,sum(nvl(f2,0)) f2,sum(nvl(f3,0)) f3,sum(nvl(f4,0)) f4,time
from
(select * from A
union all
select * from B)
group by time

------解决方案--------------------
如果不想写字段 如果不用存储过程 单纯的用sql 貌似很难
------解决方案--------------------
探讨

SQL code

create table taba
(f1 int, f2 int, f3 int, f4 int, times varchar(7))

insert into taba
select 1,2,null,null,'12:00' union all
select 3,null,4,null,'13:00'

create table tabb
(f1 int, f2 in……

------解决方案--------------------
[Quote=引用:]
补充一点:
SQL code
SQL code
select nvl(a.f1, b.f1) f1 ,
       nvl(a.f2,b.f2) f2 ,
       nvl(a.f3,b.f3) f3 ,
       nvl(a.f4,b.f4) f4 ,a.time
from testA a , testB b where a.time=b.time ;

------解决方案--------------------