select custid from sales.orders where empid in(1,2,3,4,5)
group by custid
having count(distinct empid)=5最后一句 having count(distinct empid)=5 作用试想custid与empid的如下
custid empid
1 1
1 2
2 2
2 3
3 1
3 4
4 5
。
87 8
select custid from sales.orders where empid in(1,2,3,4,5)
group by custid 这一句查出的数据 1,2,3,4 empid只在1-5之间的custid
假设结果为1,2,3,4
那么distinct empid 结果为1,2,3,4,5
count(distinct empid)结果就是5呀。
可我用其它数字也可以查出数据
那么这一句限制的是什么呢
group by custid
having count(distinct empid)=5最后一句 having count(distinct empid)=5 作用试想custid与empid的如下
custid empid
1 1
1 2
2 2
2 3
3 1
3 4
4 5
。
87 8
select custid from sales.orders where empid in(1,2,3,4,5)
group by custid 这一句查出的数据 1,2,3,4 empid只在1-5之间的custid
假设结果为1,2,3,4
那么distinct empid 结果为1,2,3,4,5
count(distinct empid)结果就是5呀。
可我用其它数字也可以查出数据
那么这一句限制的是什么呢
总体:找出empid (1,2,3,4,5)全部关联的custid最后一句 having count(distinct empid)=5 作用
筛选只保留按custid汇总,不重复的empid个数=5的,因where中只保留了5个empid,就能得到总体要求了
这一句是对你之前查询出来的结果进行过滤,只显示出每个custid 下不同的empid为5的结果
group by custid --分组聚合之后
having count(distinct empid)=5 --对应唯一的empid 计数 = 5 的为筛选项
五个不同的,distinct empid--就是箱通风的 empid只算一次,去掉重复的
empid in(1,2,3,4,5) 这也是一个条件啊!总的来说就是找在 1 2 3 4 5 雇员那里都买过东西的客户。