mysql-全表累加統計
實現思路
1)定義使用者變數,設定初始值
2)累加賦值,所求值作為顯示列
SELECT
create_time,total_num,@total_num := 0,
@total_num := @total_num + stn AS stn
FROM (
SELECT
create_time,total_num,sum(total_num) stn
FROM opm_merchant_coupon WHERE activity_id=4020000000220
GROUP BY create_time
order by create_time
) AS temp,(SELECT @total_num := 0) t
說明
mysql-分組累加統計
實現思路
1)基於父查詢傳遞的值進行範圍性的子查詢,然後拼接出想要的欄位(子查詢作為顯示列)
2)父查詢最終顯示每一行資料
簡化-範例
SELECT a, b, c,
(SELECT SUM(c) FROM sss WHERE a= A.a AND b<= A.b) t
FROM sss A;
說明
其它-範例
SELECT
omc.id, omc.merchant_code, omc.merchant_name, omc.user_code, omc.activity_id, omc.activity_name,
omc.parklot_range_code, omc.parklot_range_name, omc.send_type, omc.buy_type, omc.forward_time,
omc.rule_type,
omc.info, omc.time_type, omc.valid_start_time, omc.valid_end_time, omc.delay_time,
omc.valid_time, omc.total_num, omc.remain_num, omc.create_time, omc.op_time, omc.sys_time,
sca.total_num-(SELECT SUM(total_num) FROM opm_merchant_coupon
WHERE activity_id = omc.activity_id AND create_time <= omc.create_time) couponRemainNum
FROM opm_merchant_coupon omc
left join sys_coupon_activity sca on omc.activity_id=
sca.id
where 1=1
and omc.merchant_name like CONCAT('%','大廈中心','%' )
order by omc.create_time desc
limit 0,10