如何在MySQL中撤消having子句的组效果?

时间:2016-03-18 09:49:38

标签: mysql sql join having

enter image description here

enter image description here

查询:对于经过超过三架飞机认证的每位飞行员,找到他/她认证的每架飞机的开斋节和最大巡航范围。

查询我写道:

select certified.eid,cruising_range
from certified join employee 
on employee.eid=certified.eid
join aircraft
on certified.aid=aircraft.aid
having count(certified.eid)>3;

输出:

enter image description here

它仅显示一个巡航范围,但 id 10 的飞行员已通过 4架飞机认证。如何获得其他行?

2 个答案:

答案 0 :(得分:2)

你的问题实际上分为两部分:

  1. 找到经过三架以上飞机认证的飞行员

    Dictionary<string,decimal> packageDict= new Dictionary<string,decimal>();
    
    packageDict.AddKeyIfNotExists("demoKey",500);//will add in dictionary
    packageDict.AddKeyIfNotExists("demoKey1", 200);//will add in dictionary
    packageDict.AddKeyIfNotExists("demoKey", 6);//will not add in dictionary
    
  2. 查找这些飞机的续航范围

    SELECT eid FROM certified GROUP BY eid HAVING COUNT(*) > 3
    
  3. 结合两者:

    SELECT eid, cruising_range FROM aircraft JOIN certified USING (aid) ...
    

答案 1 :(得分:0)

使用group by certified.eid

  

count是一个聚合运算符,只返回一行   (没有分组条款)

select certified.eid,cruising_range
from certified join employee 
on employee.eid=certified.eid
join aircraft
on certified.aid=aircraft.aid
group by certified.eid`
having count(certified.eid)>3;

没有group by aggregate函数只返回一行