简体   繁体   English

MySQL的查询总和添加每个对方的结果?

[英]Mysql Query SUM adding each to eachother result?

Sorry if my question makes no sense. 对不起,如果我的问题没有道理。 Not sure if we can do this with mysql only. 不知道我们是否只能使用mysql。 Lets say I have this query: 可以说我有这个查询:

SELECT SUM(win) * 100 as win_profit, date, uid FROM `tips` WHERE uid = 60 AND placed = 1 GROUP by date

This would obviously get the sum of the win column each day that is in the database. 显然,这将每天获得数据库中获胜栏的总和。

Lets say the database had: 可以说数据库有:

|___win___|____date____|
|   10    | 2014-04-16 |
|   10    | 2014-04-16 |
|   10    | 2014-04-17 |
|   10    | 2014-04-18 |
|   10    | 2014-04-18 |
|   10    | 2014-04-18 |
|   10    | 2014-04-19 |
|   10    | 2014-04-19 |
|   10    | 2014-04-19 |

This would result: 这将导致:

20
10
30
30

How can I get it to result so each adds up, mysql query only. 我如何得到它的结果,所以每个加起来,仅mysql查询。 So the result would be: 因此结果将是:

20
30
60
90

You could get all distinct dates, and LEFT JOIN to find the sum of all values up to that date; 您可以获取所有不同的日期,然后LEFT JOIN可以找到该日期之前所有值的总和。 I kept the 100 multiplier from your sample query, but you need to remove it to get a result matching your desired result. 我保留了示例查询中的100乘数,但是您需要删除它才能获得与所需结果相匹配的结果。

SELECT 100 * SUM(b.win), a.date 
FROM (SELECT DISTINCT date FROM `tips`) a
LEFT JOIN tips b ON a.date >= b.date
GROUP BY a.date
ORDER BY a.date

An SQLfiddle to test with . 要使用进行测试的SQLfiddle

This could be another way to do it... 这可能是另一种方式...

SET @full_sum=0;
SELECT @full_sum+SUM(win) as win_profit, date as this_date, uid, 
@full_sum:=(SELECT @full_sum+SUM(win) 
            FROM `testing` WHERE uid = 60 
            GROUP by date HAVING date=this_date) 
FROM `testing` WHERE uid = 60 GROUP by date;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM