昨天由于需要统计某一时间段内的数据,需要两张关联表的数据和,
表A的sql查询结果
select count(*)
from 表A a
where a.task_type = 2
and a.create_time >= to_date('2017-01-30', 'yyyy-MM-dd')
and a.create_time < to_date('2017-07-07', 'yyyy-MM-dd')
表B的sql查询结果
select count(*)
from 表B b
where b.task_type = 2
and b.create_time >= to_date('2017-01-30', 'yyyy-MM-dd')
and b.create_time < to_date('2017-07-07', 'yyyy-MM-dd')
求两条sql之和,使用union all函数
select count(*)
from (select *
from 表A A
where a.task_type = 2
and a.create_time >= to_date('2017-01-30', 'yyyy-MM-dd')
and a.create_time < to_date('2017-07-07', 'yyyy-MM-dd')
union all
select *
from 表B b
where b.task_type = 2
and b.create_time >= to_date('2017-01-30', 'yyyy-MM-dd')
and b.create_time < to_date('2017-07-07', 'yyyy-MM-dd'));
此处UNION ALL 这个指令的目的也是要将两个 SQL 语句的结果合并在一起。 UNION ALL 和UNION 不同之处在于UNION ALL 会将每一个符合条件的资料都列出来,无论资料值有无重复