Mysql的行值总和取决于其他相同的字段

时间:2016-11-18 06:53:48

标签: android mysql json phpmyadmin

我需要具有相应日期的每家公司的现金和信贷总和

+-----------+------------+---------------+--------+----------
| ID        |  SaleDate  | Cash          | Credit |company  |
+-----------+------------+---------------+--------+----------
|         1 | Nov-12-2016| 100           |     150|company 1|
|         2 | Nov-12-2016| 200           |     100|Company 1|
|         3 | Nov-13-2016| 150           |     200|Company 2|
|         4 | Nov-13-2016| 250           |     250|Company 2|
|         5 | Nov-14-2016| 80            |      50|Company 3|
|         6 | Nov-14-2016| 500           |     600|Company 3|
|           | Nov-15-2016| 300           |     300|Company 4|
+-----------+------------+---------------+--------+---------+

Desired OutPut

+------------+---------------+--------+----------
|  SaleDate  | Cash          | Credit |company  |
+------------+---------------+--------+----------
| Nov-12-2016| 300           |     250|company 1|
| Nov-13-2016| 400           |     450|Company 2|
| Nov-14-2016| 580           |     650|Company 3|
| Nov-15-2016| 300           |     300|Company 4|
+-----------+------------+---------------+------+
我不喜欢'知道如何做到这一点,请提前帮助我找到解决方案......

2 个答案:

答案 0 :(得分:0)

select  SaleDate,sum(Cash)Cash,sum(Credit)Credit,company   
from tbl_name group by  
SaleDate,company 

答案 1 :(得分:0)

试试这个

 SELECT SUM(cash) AS Total_cash,SUM(Credit) AS Total_credit,*
 FROM `table_name` 
 GROUP BY SaleDate