mysql group by去重_MySQL本周总结——group by去重和自联结理解

1、group by 理解:

从一个例题开始:

已知表格:Product(maker, type, model)

Get the makers who produce only one product type and more than one model. Output: maker, type.

即找出:1个type,>1个model的maker,输出: maker, type

答案:

select distinct maker, type

from Product

where maker in

(select maker from Product

group by maker

having count(distinct type)=1 and count(distinct model)>1);

分析:

① SQL中distinct和group by去重区别:

distinct必须放在开头,将所有查询的字段进行对比,所有字段都完全相同才会去重;

group by 根据字段进行去重,字段相同就会去重。

② 当group by 字段1,字段2,(注意整个表中不止这两个字段),表示数据集中,字段1相等,字段2也相等的数据归为一组,只显示一条数据。那么你可以对字段3进行统计(求和,求平均值等);

对应的,group by 单个字段:这个字段相等的数据将归为一组,只显示一条记录。

③ 理解:group by

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值