0

我需要有关我一直在努力完成的 MySQL 查询的帮助。我相信这并不难弄清楚,但我就是看不到它。

我需要再次对一列求和,即使它已经运行了一次并存储该值并将其显示在它旁边的新列中?

此查询显示 2 行单独的记录,其中“fcpd”和“CPDTotal”列是该问题的主要部分。“CPDTotal”列的第一行记录的值为 0.5,第二行也是如此。我需要做的就是取 0.5 + 0.5 给我总共 1 并在执行后将其显示在新列中。

    SELECT
    mdl_user_info_data.data,
    mdl_user.firstname, 
    mdl_user.lastname, 
    mdl_grade_grades.itemid, 
    mdl_grade_items.itemname,
    mdl_user_info_data.id,
    mdl_grade_grades.timecreated AS DATE,
    mdl_quiz.fcpd,
    SUM(mdl_quiz.fcpd) as CPDTotal
    FROM mdl_grade_grades

    INNER JOIN mdl_user ON mdl_grade_grades.userid = mdl_user.id
    INNER JOIN mdl_grade_items ON mdl_grade_grades.itemid = mdl_grade_items.id
    INNER JOIN mdl_quiz ON mdl_grade_items.itemname = mdl_quiz.name
    INNER JOIN mdl_course ON mdl_grade_items.courseid = mdl_course.id
    INNER JOIN mdl_user_info_data ON mdl_user.id = mdl_user_info_data.userid


    WHERE mdl_user_info_data.fieldid = 1
    AND mdl_grade_items.itemname IS NOT NULL
    AND mdl_user.annualCPDReportActive = 'Y'
    AND mdl_user.id = 1025
    AND (mdl_course.category = 27)
    GROUP BY  mdl_user.firstname, 
    mdl_user.lastname,
    mdl_user_info_data.data,
    mdl_grade_items.itemname
4

2 回答 2

0

您可以通过技巧在 MySQL 中实现这一点 - WITH ROLLUP

SELECT
mdl_user_info_data.data,
mdl_user.firstname, 
mdl_user.lastname, 
mdl_grade_grades.itemid, 
mdl_grade_items.itemname,
mdl_user_info_data.id,
mdl_grade_grades.timecreated AS DATE,
mdl_quiz.fcpd,
SUM(mdl_quiz.fcpd) as CPDTotal
FROM mdl_grade_grades

INNER JOIN mdl_user ON mdl_grade_grades.userid = mdl_user.id
INNER JOIN mdl_grade_items ON mdl_grade_grades.itemid = mdl_grade_items.id
INNER JOIN mdl_quiz ON mdl_grade_items.itemname = mdl_quiz.name
INNER JOIN mdl_course ON mdl_grade_items.courseid = mdl_course.id
INNER JOIN mdl_user_info_data ON mdl_user.id = mdl_user_info_data.userid


WHERE mdl_user_info_data.fieldid = 1
AND mdl_grade_items.itemname IS NOT NULL
AND mdl_user.annualCPDReportActive = 'Y'
AND mdl_user.id = 1025
AND (mdl_course.category = 27)
GROUP BY  mdl_user.firstname, 
mdl_user.lastname,
mdl_user_info_data.data,
mdl_grade_items.itemname
WITH ROLLUP

这不会为您提供新列,但您的结果将在每个组之后包含相应的总和行。

于 2013-08-22T10:41:40.027 回答
0

我可能会误解它,但是如果值没有改变,你可以将它乘以 2 吗?

于 2013-08-22T10:42:12.097 回答