如何总结所有标题甚至其他标题都是空的

时间:2014-02-22 13:05:34

标签: mysql sql

我的查询目标是对(3)表中的所有字段求和,但是如果其他一些标题为空final_total_sum为空,则生成final_total_sum输出时遇到问题....但如果所有titles都不为空,我的查询会生成final_total_sum(output)

我要做的是即使其他标题都是空的,我的查询仍然可以生成final_total_sum(output)

http://s38.photobucket.com/user/eloginko/media/output_zpsfcab9d54.png.html

当前查询:

SELECT *,
    ROUND(interview_sum +
          other_sum +
          edu_attain2_sum +
          experience2_sum +
          trainings2_sum +
          eligibility2_sum) AS final_total_sum
FROM (
    SELECT
        ROUND((SELECT SUM(t2.inttotal)
         FROM app_interview2 AS t2 
         WHERE t2.atic = t.atic)/7,1)
         AS interview_sum,

        ROUND((SELECT SUM(o2.ototal)
         FROM other_app2 AS o2 
         WHERE o2.oaic = t.atic)/7,1)
         AS other_sum,

        ROUND((SELECT SUM(s1.edu_attain2)
         FROM qual_stan2 AS s1 
         WHERE s1.oaic2 = t.atic)/7,1)
         AS edu_attain2_sum,

        ROUND((SELECT SUM(s2.experience2)
         FROM qual_stan2 AS s2 
         WHERE s2.oaic2 = t.atic)/7,1)
         AS experience2_sum,

        ROUND((SELECT SUM(s3.trainings2)
         FROM qual_stan2 AS s3 
         WHERE s3.oaic2 = t.atic)/7,1)
         AS trainings2_sum,

        ROUND((SELECT SUM(s4.eligibility2)
         FROM qual_stan2 AS s4 
         WHERE s4.oaic2 = t.atic)/7,1)
         AS eligibility2_sum,

        t.atid,
        t.atic,
        t.atname,
        t.region,
        t.town,
        t.uniq_id,
        t.position,
        t.salary_grade,
        t.salary

    FROM app_interview2 AS t
    WHERE uniq_id = '$q'
    GROUP BY t.atname 
    HAVING COUNT(DISTINCT t.atic)) subq

2 个答案:

答案 0 :(得分:0)

像SUM一样使用(ifnull(t2.inttotal,0))

答案 1 :(得分:0)

尝试:

   ROUND( ifnull(interview_sum,0) +
          ifnull(other_sum,0) +
          ifnull(edu_attain2_sum,0) +
          ifnull(experience2_sum,0) +
          ifnull(trainings2_sum,0) +
          ifnull(eligibility2_sum,0)) AS final_total_sum

在SQL x + NULL中总是给出NULL,ifnull函数将空值转换为0