MySQL SUM(列)+(带SUM的子查询)

时间:2016-08-23 11:15:50

标签: mysql sql

我想制作一个为团队加分的查询。通过对具有相同team-id的另一个表的列+ SUM进行SUM来累加这些点。我试着这样写:

SELECT 
    k.id, 
    s.fylke, 
    s.Kommune, 
    s.Skolenavn, 
    k.schoolid, 
    k.number, 
    k.letter, 
    SUM(e.amount) + (SELECT SUM(poeng) FROM oppdrag WHERE klasseid=k.id ) AS poeng 
FROM skoler AS s, klasser AS k, etappe AS e 
WHERE s.id=k.schoolid AND k.id=e.klasseid AND e.year='2016'
GROUP BY k.id 
ORDER BY poeng

问题在于,当我以这种方式编写它时,如果团队在表“oppdrag”中有一个条目,它会给出正确的分数,否则它只返回NULL作为点(poeng)。

4 个答案:

答案 0 :(得分:2)

如果子查询返回NULL,则会导致尝试将数字与NULL相加,这会使NULL返回。

要解决此问题,您可以尝试使用IFNULL函数,如果查询未返回任何值,则会将其替换为0

SELECT 
    k.id, 
    s.fylke, 
    s.Kommune, 
    s.Skolenavn, 
    k.schoolid, 
    k.number, 
    k.letter, 
    SUM(e.amount) + IFNULL((SELECT SUM(poeng) FROM oppdrag WHERE klasseid=k.id ), 0) AS poeng 
FROM skoler AS s, klasser AS k, etappe AS e 
WHERE s.id=k.schoolid AND k.id=e.klasseid AND e.year='2016'
GROUP BY k.id 
ORDER BY poeng

答案 1 :(得分:2)

我从你的select语句中避免使用该子查询,它会破坏你的表现。此外,您应该使用正确的连接语法(您使用的样式很古老)。试试这样的事情;

SELECT 
    k.id, 
    s.fylke, 
    s.Kommune, 
    s.Skolenavn, 
    k.schoolid, 
    k.number, 
    k.letter, 
    SUM(e.amount) + ISNULL(SUM(od.poeng),0) AS poeng 
FROM skoler AS s
INNER JOIN klasser AS k 
    ON s.id = k.schoolid
INNER JOIN etappe AS e 
    ON k.id = e.klasseid
LEFT JOIN oppdrag od 
    ON od.klasseid = k.id
WHERE e.year='2016'
GROUP BY k.id 
ORDER BY poeng

答案 2 :(得分:1)

首先,学习使用显式JOIN语法。简单规则:从不FROM子句中使用逗号。 始终使用JOIN明确ON

然后,您可以通过在FROM子句中包含子查询来解决您的问题:

SELECT ske.*, ske.amount + COALESCE(od.amount, 0) as poenb
FROM (SELECT k.id, s.fylke, s.Kommune, s.Skolenavn, k.schoolid, 
             k.number, k.letter, SUM(e.amount) as amount
      FROM skoler s JOIN
           klasser k
           ON s.id = k.schoolid JOIN
           etappe e 
           ON k.id = e.klasseid AND e.year = '2016' 
      GROUP BY k.id 
     ) ske LEFT JOIN
     (SELECT od.klasseid, SUM(od.poeng) as amount
      FROM oppdrag od
      GROUP BY od.klasseid
     ) od
     ON od.klasseid = k.id
ORDER BY poeng;

答案 3 :(得分:1)

使用显式JOIN语法。此外,包括GROUP BY子句中未聚合的所有列。仅仅因为MySQL没有产生错误,这并不意味着以你做它的方式做它是明智的。不包括通过选择随机值而未在组中聚合的所有列。

SELECT 
    k.id, 
    s.fylke, 
    s.Kommune, 
    s.Skolenavn, 
    k.schoolid, 
    k.number, 
    k.letter, 
    COALESCE(SUM(e.amount), 0) + COALESCE(SUM(o.poeng), 0) AS poeng 
FROM skoles AS s
INNER JOIN klasser AS k ON s.id = k.schoolid
INNER JOIN etappe AS e ON k.id = e.klasseid
LEFT JOIN ( SELECT klasseid, SUM(poeng) AS poeng FROM oppdrag ) AS o ON o.klasseid = k.id
WHERE e.year = '2016'
GROUP BY k.id, s.fylke, s.Kommune, s.Skolenavn, k.schoolid, k.number, k.letter
ORDER BY poeng