sql 语句中的DISTINCT以及在count中的使用

原文链接:https://www.cnblogs.com/tanshuai1001/p/8761378.html

https://baijiahao.baidu.com/s?id=1709966309120511971&wfr=spider&for=pc

district必须放在所有字段前面:

SELECT DISTINCT student,class FROM courses 

单字段时按照字段筛选,多字段是以所有字段的值作为key来筛选。

SELECT class,COUNT(DISTINCT student,class) as c FROM courses GROUP BY class 

在count中的使用也是一样。相当于先通过 select district 出来后再进行count。

 

count是不能统计多个字段的,下面的SQL在SQL Server和Access中都无法运行。

select count(distinct name, id) from A;
若想使用,请使用嵌套查询,如下:

select count(*) from (select distinct xing, name from B) AS M;

 

select customer_id ,count(product_key)
select customer_id ,count(distinct product_key)
select customer_id ,count(customer_id )
select customer_id ,count(distinct customer_id )
from Customer
group by customer_id
order by customer_id
四个结果1、3相同,其他都不同

                

 

 

 

 

posted @   yinghualeihenmei  阅读(388)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示