0

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

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|

如果我有三个 id,那么他将它乘以 3!

每次如果我添加一个“id_service”,他就会乘以 id_service 的数量

对于每个注释,我有 3 次相同的查询。注释为 1、2、3。这些示例仅适用于注释 2

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

提前 THX

4

1 回答 1

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;
于 2013-04-19T11:27:22.760 回答