Mawia HL
Mawia HL

Reputation: 3665

Why mysql sum not working in mysql view table?

I am using the following query to get total score from mysql view table

Select regd, date, subject, (t_scored+w_scored+f_scored+cce_scored) as Total
from
exam_view
where regd='3'
group by subject

In the total column I only get NULL where there are field values for each scored field. Please can anyone help me?

Upvotes: 0

Views: 241

Answers (1)

gts
gts

Reputation: 627

Use an inline query to get separate sums, and the outer query to add up the sums

SELECT regd, date, subject, SUM(t_scored+w_scored+f_scored+cce_scored) AS total
FROM
(
SELECT regd, date, subject, SUM(t_scored) as t_scored, SUM(w_scored) as w_scored, SUM(f_scored) AS f_scored, SUM(cce_scored) AS cce_scored
FROM
exam_view
WHERE regd='3'
) AS temp
GROUP BY subject

Upvotes: 1

Related Questions