[英]Summing multiple row in mysql
I have a mysql table name request and the request has the following fields 我有一个mysql表名请求,请求包含以下字段
id mem_id amount status rtype
1 11 200 0 ph
2 12 200 0 ph
3 13 400 0 ph
4 14 200 0 ph
5 15 300 0 ph
what i wanted to do with this table is if a user insert another data and the rtype = gh and amount = 600 i want mysql to pick from the request table and sum the amount in order to get the new requested amount which is 600 like a peer to peer donation 我想用这个表做的是,如果用户插入另一个数据并且rtype = gh和amount = 600我希望mysql从请求表中选择并加总金额以获得新请求的金额600就像一个同行捐赠
The result can look like 结果可能看起来像
id mem_id amount status rtype
1 11 200 0 ph
2 12 200 0 ph
4 14 200 0 ph
or 要么
id mem_id amount status rtype
1 11 200 0 ph
3 13 400 0 ph
i have this query but it returns an empty result from my phpmyadmin 我有这个查询,但它从我的phpmyadmin返回一个空结果
SELECT id, mem_id, SUM(amount), status, rtype FROM tbl_request WHERE rtype = 'ph' AND status = 0 AND amount = 6000 GROUP BY id;
please any help 请任何帮助
Thanks in advance 提前致谢
You can do cumulative sum using a mysql variable. 您可以使用mysql变量进行累积求和。
SELECT
*
FROM
(SELECT @var:=600) a,
(SELECT
tbl_request.*,
if((@var:=@var - amount) > 0, amount, amount + @var) as my_amount
FROM
tbl_request
WHERE
rtype = 'ph' AND status = 0
ORDER BY amount ASC) AS b
WHERE
my_amount > 0
from here : https://stackoverflow.com/a/11935359/6124896 从这里: https : //stackoverflow.com/a/11935359/6124896
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.