union 的问题

boytomato 2007-06-24 02:02:06
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

...全文
310 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yoncen 2007-06-25
  • 打赏
  • 举报
回复
用union all 后保证不会重复!
------------------------
應該是union會過濾掉重複數據,union all則不會過濾重複數據.
如果不需要過濾重複數據時,用union all比union效率要高得多!
fa_ge 2007-06-25
  • 打赏
  • 举报
回复
接分吧
ojuju10 2007-06-25
  • 打赏
  • 举报
回复
用union all 后保证不会重复!
sp4 2007-06-25
  • 打赏
  • 举报
回复
不再做回答了
fwacky 2007-06-25
  • 打赏
  • 举报
回复
select * from b
union all
select * from a
where not exists (
select 1 from b where
cur_name =a.cur_name
)
y_dong119 2007-06-25
  • 打赏
  • 举报
回复
Haiwer(海阔天空) 正解


都正解答了
sqllong 2007-06-24
  • 打赏
  • 举报
回复
Haiwer(海阔天空) 正解

学习
boytomato 2007-06-24
  • 打赏
  • 举报
回复
数据量比较大
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]

大家看看那种效率高点
昵称被占用了 2007-06-24
  • 打赏
  • 举报
回复
seelct * from b
union all
select * from a
where not exists (
select 1 from b where
cur_name =a.cur_name
)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧