Mysql - 在我的Calc查询中没有添加

时间:2013-04-19 09:45:19

标签: mysql

我将对笔记进行计算,并通过MySQL查询实现这一点。 这里整个查询:

SELECT DISTINCT Round(Avg(avg = 2) * Count(avg)) AS New,
                sma_famille.famille
FROM   (SELECT DISTINCT Round(Sum(note) / Count(note)) AS AVG,
                        sma_famille.famille,
                        sma_agents.nom
        FROM   sma_notes_conso
               INNER JOIN sma_famille
                       ON sma_famille.id_service
               INNER JOIN sma_agents
                       ON sma_notes_conso.id_agent = sma_agents.id_agent
               INNER JOIN sma_service_activite
                       ON sma_service_activite.id_activite =
                          sma_notes_conso.id_activite
               INNER JOIN sma_service
                       ON sma_service.id_service
        WHERE  sma_service.id_entites IN( 20 )
               AND sma_famille.id_service IN( 988, 989, 990 )
               AND sma_service_activite.id_famille = sma_famille.id_famille
               AND sma_service_activite.id_service = sma_famille.id_service
               AND sma_service_activite.id_service = sma_service.id_service
               AND date_conso = '2013-04-03'
        GROUP  BY sma_famille.famille,
                  sma_agents.nom) AS FN
       INNER JOIN sma_famille
               ON sma_famille.id_service
       INNER JOIN sma_service
               ON sma_service.id_service
WHERE  sma_service.id_entites IN( 20 )
       AND sma_famille.id_service IN( 988, 989, 990 )
       AND sma_service.id_service = sma_famille.id_service
       AND FN.famille = sma_famille.famille
GROUP  BY FN.famille  

如果我只有一个id_service,则查询正常工作: 这是第一个:

    AND sma_famille.id_service IN(988)

我得到了这些结果集:

    |9  |Math|
    |13 |English|
    |2  |Bio|

这里是第二个

    AND sma_famille.id_service IN(989)

    |5  |Math|
    |8  |English|
    |0  |Bio|

如果我同时使用它们,则将它乘以2

    AND sma_famille.id_service IN(988,989)

我得到了这些结果集:

    |28 |Math|
    |42 |English|
    |4  |Bio|

但是我不想要乘法,我想要添加'id_service',例如:

    |14  |Math|
    |21  |English|
    |2   |Bio|

如果我有三个身份证,那就比他乘以3 !!!

每次如果我添加'id_service',他都会乘以id_service的数量

对于每个Note,我有3次相同的Query。注释是1,2,3。 这些示例仅适用于注释2

任何人都可以看到问题吗?

提前THX

1 个答案:

答案 0 :(得分:0)

我想我找到了你的问题:

您没有正确设置INNER JOIN。因此,您的查询执行了笛卡尔连接并将值相乘。以下应该是正确的:

SELECT DISTINCT round( avg( AVG =1 ) * count( AVG ) ) AS New, sma_famille.famille
                            FROM (...
                            ) AS FN
                            INNER JOIN sma_famille ON sma_famille.id_service = FN.id_service -- added join criteria
                            INNER JOIN sma_service ON sma_service.id_service = FN.id_service -- added join criteria
                            WHERE sma_service.id_entites IN(20)
                            AND sma_famille.id_service IN(988,989,990) 
                            AND sma_service.id_service = sma_famille.id_service
                            AND FN.famille = sma_famille.famille
                            GROUP BY FN.famille;