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

MySQL implementation phase cumulative SQL writing method (code example)

不言
Release: 2019-01-15 10:43:05
forward
5467 people have browsed it

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>
Copy after login

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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template