a 表
cu_id cur_name date
1 abc 2006
2 def 2007
5 efg 2008
b 表
cur_id cur_name date
1 abc 2007
1 abc 2007
3 def 2008 用a union b 时
线保证 b 里边的数据
也就是 b 里边要是有的话
union 后的结果就是 b 的数据
a 表和表合并后为1 abc 2007
1 abc 2007
3 def 2008
5 efg 2008
cu_id cur_name date
1 abc 2006
2 def 2007
5 efg 2008
b 表
cur_id cur_name date
1 abc 2007
1 abc 2007
3 def 2008 用a union b 时
线保证 b 里边的数据
也就是 b 里边要是有的话
union 后的结果就是 b 的数据
a 表和表合并后为1 abc 2007
1 abc 2007
3 def 2008
5 efg 2008
union all
select * from a
where not exists (
select 1 from b where
cur_name =a.cur_name
)
10W以上
我是这样些的先查处来a 表有但b 表没有的数据然后在和
b 表取合集select c.* ,d.user_name from
(select flow_time,flow_name ,flow_bc_id,flow_dz_id,flow_dept_child_id from flow_info_list a
left join kqInfoList b on a.flow_name=b.kq_id and convert(nvarchar(10),b.kq_date,126)='2007-06-23'
and b.kq_bc=1 and b.kq_dz=1
where b.kq_cur_id is null and
a.flow_bc_id=1 and a.flow_dz_id=1
and convert(nvarchar(10),a.flow_time,126)='2007-06-23'
union all
select kq_date,kq_id,kq_bc,kq_dz,kq_deptchild_id from KqInfoList
where convert(nvarchar(10),kq_date,126)='2007-06-23'
and kq_bc=1 and kq_dz=1 ) c inner join kquserInfo d
on c.flow_name=d.[user_id]大家看看那种效率高点
都正解答了
union all
select * from a
where not exists (
select 1 from b where
cur_name =a.cur_name
)
------------------------
應該是union會過濾掉重複數據,union all則不會過濾重複數據.
如果不需要過濾重複數據時,用union all比union效率要高得多!