mysql关键字distinct笔记

本文深入探讨了SQL中distinct关键字的应用,解释了其在不同查询条件下的工作原理,并展示了如何利用distinct结合group by和count函数进行数据统计,提供了解决实际问题的实用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

table

id name

1 a

2 b

3 c

4 c

5 b

select distinct name from table

得到的结果是:


name

a

b

c

select distinct name, id from table


结果会是:


id name

1 a

2 b

3 c

4 c

5 b


distinct怎么没起作用?作用是起了的,不过他同时作用了两个字段,也就是必须得id与name都相同的才会被排除……

如果要按name进行统计应这样写才是对的 就是count函数结合group by

select *, count(distinct name) from table group by name


结果:


id name count(distinct name)

1 a 1

2 b 1

3 c 1



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值