我需要帮助完成我一直在努力完成的MySQL查询。我确信不难发现,但我看不到它。
我需要再次SUM一列,即使它已运行一次并存储该值并将其显示在旁边的新列中?
此查询显示2个单独的记录行,其中'fcpd'和'CPDTotal'列是此问题的主要部分。列'CPDTotal'的第1行记录的值为0.5,第2行也是如此。我需要做的就是取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
答案 0 :(得分:0)
你可以通过技巧 - 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
这不会为您提供新列,但您的结果将在每个组后面包含相应的求和行。
答案 1 :(得分:0)
我可能会误解它,但是如果值没有改变,你可以将它乘以2吗?