Home >Database >Mysql Tutorial >MySQL implementation phase cumulative SQL writing method (code example)

MySQL implementation phase cumulative SQL writing method (code example)

不言
不言forward
2019-01-15 10:43:055437browse

The content of this article is about the cumulative SQL writing method (code example) in the MySQL implementation phase. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Recently there is a need: to count the balance after daily recharge/consumption. For this need, it is actually very simple. You only need to calculate the balance after each recharge/consumption, and then save it. Just come down. But for this kind of demand, a single sql can handle it, and there is no need to create redundant fields.

The chart showing this scenario may be more straightforward

MySQL implementation phase cumulative SQL writing method (code example)

Required results

MySQL implementation phase cumulative SQL writing method (code example)

Writing method one:

select t.*
,(select sum(price) from t_charge temp where temp.date <p><strong>Writing method two: </strong></p><pre class="brush:php;toolbar:false">select t.*, sum(temp.price) as total_price
from t_charge t,t_charge temp
where t.date <p class="comments-box-content"></p>

The above is the detailed content of MySQL implementation phase cumulative SQL writing method (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete