Чтобы получить текущую сумму:
SELECT c.id AS campaign_id, c.amount AS campaign_amount, c.warning_trigger,
COUNT(cc.id) as code_count, DATEDIFF(c.end_date, CURDATE()) as days_remain,
SUM(c.amount) as row_total, @r := @r + SUM(c.amount) AS running_sum
FROM (
SELECT @r := 0
) vars,
campaign c, …
Чтобы получить общий итог (и все остальные агрегаты) в качестве дополнительной записи:
SELECT *
FROM (
SELECT c.id AS campaign_id, c.amount AS campaign_amount, c.warning_trigger,
COUNT(cc.id) as code_count, DATEDIFF(c.end_date, CURDATE()) as days_remain,
SUM(c.amount) as row_total
FROM campaign c, …
GROUP BY
c.id WITH ROLLUP
) q
ORDER BY
campaign_amount ASC
Чтобы получить общую сумму в дополнительном поле:
SELECT c.id AS campaign_id, c.amount AS campaign_amount, c.warning_trigger,
COUNT(cc.id) as code_count, DATEDIFF(c.end_date, CURDATE()) as days_remain,
SUM(c.amount) as row_total,
(
SELECT SUM(c.amount)
FROM campaign c, …
-- the same but without the GROUP BY and ORDER BY clauses.
)
FROM campaign c, …
GROUP BY
c.id
ORDER BY
campaign_amount ASC