MySQL 中一组行的累积和计算
问题陈述:
确定来自包含 ID 列的查询结果集的一天中所有小时内每个 ID 的累积总和,天、小时和金额。
答案:
SELECT IF(@prev_id = c.id AND @prev_day = c.day ,@cumtotal := @cumtotal + c.amount ,@cumtotal := c.amount) AS cumulative_total , @prev_id := c.id AS `id` , @prev_day := c.day AS `day` , c.hr , c.amount AS `amount' FROM ( SELECT @prev_id := NULL , @prev_day := NULL , @subtotal := 0 ) i JOIN ( select id, day, hr, amount from ( //multiple joins on multiple tables)a left join (//unions on multiple tables)b on a.id=b.id ORDER BY 1,2,3 ) c
附加说明:
以上是如何在没有解析函数的情况下计算MySQL中的累积和?的详细内容。更多信息请关注PHP中文网其他相关文章!