关于union 与 union all

leehong0704 2010-02-21 01:42:44
union 与 union all 怎么过滤某一字段重复的数据的。
...全文
92 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sweet-Tang 2010-02-21
  • 打赏
  • 举报
回复
union 过滤重复数据
union all 不会过滤重复数据
leehong0704 2010-02-21
  • 打赏
  • 举报
回复
比较麻烦的,不过现在不用这个
union 都不用了,直接2个left join,left join就搞定了 谢谢
我被自己思路限制了。
百年树人 2010-02-21
  • 打赏
  • 举报
回复
用union可以去重复
if object_id('[ta]') is not null drop table [ta]
go
create table [ta]([id] int,[name] varchar(1))
insert [ta]
select 1,'a' union all
select 2,'b' union all
select 3,'a' union all
select 4,'c'
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[name] varchar(1))
insert [tb]
select 1,'a' union all
select 2,'a' union all
select 3,'b' union all
select 4,'c'

select name from ta
union
select name from tb
/**
name
----
a
b
c

(所影响的行数为 3 行)
**/

select name from ta
union all
select name from tb
/**
name
----
a
b
a
c
a
a
b
c

(所影响的行数为 8 行)
**/
chinesesword 2010-02-21
  • 打赏
  • 举报
回复
估计不行吧 union 应该可以自动 将 所有字段重复的 记录 只保留一条
某一个字段 值重复 分组取最大 值 应该还可以
ws_hgo 2010-02-21
  • 打赏
  • 举报
回复
select col1 from table
union
select col1 from table1
百年树人 2010-02-21
  • 打赏
  • 举报
回复
过滤重复数据用distinct、group by或者子查询

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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