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 @ 2024-01-17 18:42  yinghualeihenmei  阅读(279)  评论(0编辑  收藏  举报