在 select 语句中,用 distinct 如何对两列进行操作,如对,马上结贴

fengcl 2002-10-24 04:52:12
在 select 语句中,用 distinct 如何对两列进行操作,使这两列的组合最终是不重复的。
...全文
966 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
supsuccess 2002-10-24
  • 打赏
  • 举报
回复
select col2,col3,min(col4),min(col5)
from tab
group by col2,col3
fengcl 2002-10-24
  • 打赏
  • 举报
回复
那我如果只想对两列起作用,但又想一次 select 多列怎办
supsuccess 2002-10-24
  • 打赏
  • 举报
回复
1.distinct影响其后面的所有列出的列;
2.select distinct col2,col3 from tab
fengcl 2002-10-24
  • 打赏
  • 举报
回复
如果用 select distinct col1,col2,col3......后面还有其他列时,distinct 对哪几列进行操作。如果我有如下记录

col1 col2 col3 col4 ......

dddddd 3 1 t
ffffff 2 2 f
rrrrrr 2 2 g
tttttt 3 2 h
以 col2 和 col3 用distinct 形成具有主键功能的组合键
具体怎么写 SQL 语句
蓝天 2002-10-24
  • 打赏
  • 举报
回复
把所有不唯一的组合查出来
select col1,col2 from test group by col1,col2 having count(*) >1
把所有不唯一的组合删除
delete from test where exists select 1 from test group by col1,col2 having count(*) >1
蓝天 2002-10-24
  • 打赏
  • 举报
回复
把所有不唯一的组合查出来
select col1,col2 from test group by col1,col2 having count(*) >1
把所有不唯一的组合删除
delete from test where exists select 1 from test group by col1,col2 having count(*) >1
蓝天 2002-10-24
  • 打赏
  • 举报
回复 1
select distinct col1, col2 from yourtable
fengcl 2002-10-24
  • 打赏
  • 举报
回复
应该是用 distinct 对这两列操作后,使这两列的组合相当于主键的功能
duckcn 2002-10-24
  • 打赏
  • 举报
回复 1
select distinct columna, columnb from yourtable where ....
fengcl 2002-10-24
  • 打赏
  • 举报
回复
这两列中都有重复的
zhpsam109 2002-10-24
  • 打赏
  • 举报
回复
两列的值都没有重复的

34,590

社区成员

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

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