• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

无信不立

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

distinct数据去重关键字

在表中,可能会包含重复值。这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值。关键词 distinct用于返回唯一不同的值。

表A:

示例1

select distinct name from A

执行后结果如下:

示例2

select distinct name, id from A

执行后结果如下:

实际上是根据“name+id”来去重

示例3:统计

select count(distinct name) from A;	  --表中name去重后的数目, SQL Server支持,而Access不支持
select count(distinct name, id) from A;	  --SQL Server和Access都不支持

示例4

select id, distinct name from A;   --会提示错误,因为distinct必须放在开头

其他

distinct语句中select显示的字段只能是distinct指定的字段,其他字段是不可能出现的。例如,假如表A有“备注”列,如果想获取distinc name,以及对应的“备注”字段,想直接通过distinct是不可能实现的。
但可以通过其他方法实现关于SQL Server将一列的多行内容拼接成一行的问题讨论

posted on 2015-04-24 20:26  无信不立  阅读(1760)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3