2012-07-21 39 views
1

我有一个问题,这个查询得到错误#1111 - 无效使用组功能

SELECT SUM(ROUND(SUM(note=3)/count(note=3))) AS AVG, sma_famille.famille 
       FROM sma_notes, sma_famille, sma_agents, sma_service_activite 
       WHERE sma_famille.id_famille >=14 
       AND sma_famille.id_service =9 
       AND sma_notes.id_agent >=7 
       GROUP BY sma_famille.id_famille 

和我得到的错误信息

#1111 - Invalid use of group function 

如果有人有这种查询的经验。

这里查询的一些细节:

我我采取查询,而像这样的第一SUM(这是整个查询码)

SELECT ROUND(SUM(note =3)/count(note =3)) AS AVG, sma_famille.famille, sma_notes.id_agent 
    FROM sma_notes, sma_famille, sma_agents, sma_service_activite 
    WHERE sma_famille.id_famille >=14 
    AND sma_famille.id_service =9 
    AND sma_notes.id_agent >=7 
    AND sma_notes.id_agent = sma_agents.id_agent 
    AND sma_service_activite.id_activite = sma_notes.id_activite 
    AND sma_service_activite.id_famille = sma_famille.id_famille 
    GROUP BY sma_famille.id_famille, sma_notes.id_agent 

比我得到的结果一样:

AVG | famille | id_agent 
    3 |BEL  | 7 
    2 |BEL  | 8 
    1 |BEL  | 9 
    1 |CEL  | 7 
    2 |CEL  | 8 
    1 |CEL  | 9 

但随着我的查询谁没有工作:

SELECT SUM(ROUND(SUM(note=3)/count(note=3))) AS AVG, sma_famille.famille 
    FROM sma_notes, sma_famille, sma_agents, sma_service_activite 
     WHERE sma_famille.id_famille >=14 
     AND sma_famille.id_service =9 
     AND sma_notes.id_agent >=7 
     AND sma_notes.id_agent = sma_agents.id_agent 
     AND sma_service_activite.id_activite = sma_notes.id_activite 
     AND sma_service_activite.id_famille = sma_famille.id_famille 
     GROUP BY sma_famille.id_famille 

我想要得到这样的结果:

AVG | famille 
    6 |BEL  
    4 |CEL  

在此先感谢

Achillix

干杯

+0

阅读这里:http://stackoverflow.com/questions/2330840/mysql-invalid-use-of-group-function – 2012-07-21 07:19:09

回答

2

你应该使用的WHEREHAVING条款代替。

见下文。

考虑我有表Address的列为City & State

HAVING用于在聚合发生后检查条件。

WHERE在聚合发生之前使用。

此代码:

select City, CNT=Count(1) 
From Address 
Where State = 'MA' 
Group By City 

为您提供了MA所有城市的计数。

此代码:

select City, CNT=Count(1) 
From Address 
Where State = 'MA' 
Group By City 
Having Count(1)>5 

给你所有的MA出现6次以上的城市数量。

又读

Definition, Comparison and Difference between HAVING and WHERE Clause

Where Vs Having

+0

我把我的查询结束有计数(AVG)> 1,但我仍然得到相同的错误信息。这是沮丧:-( 或者,如果我有其他地方的查询内的错误??? – achillix 2012-07-21 07:50:30

+0

@achillix:而不是'WHERE'使用'HAVING' – 2012-07-21 07:56:41

+0

@achillix:现在它的工作?? – 2012-07-21 08:03:26

相关问题