Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何获得3个表列的总和_Mysql - Fatal编程技术网

Mysql 如何获得3个表列的总和

Mysql 如何获得3个表列的总和,mysql,Mysql,我希望输出应如下所示: 名称ID |名称|产品|地点|金额|价格|费用|价格+费用|金额-(价格+费用)| 您可以使用盲的SUM()和带汇总的来实现这一点: 2 |john| dhal | Ny | 3000 | 100 | 280 | 380| 2620 | 1 |chan|chenna | At | 2000 | 150 | 500 | 650| 135

我希望输出应如下所示:

名称ID |名称|产品|地点|金额|价格|费用|价格+费用|金额-(价格+费用)|


您可以使用盲的
SUM()
和带汇总的
来实现这一点:

2 |john| dhal  | Ny  | 3000 | 100 |   280 |            380|                     2620 |

1 |chan|chenna | At  | 2000 | 150 |   500 |            650|                     1350 |

3 |james| ghee | ca  | 2000 | 400 |   600 |            1000|                    1000 |

Total|------|-----|-----| 7000 |  650 |  1380 |           2030 |                    4970
编辑

无法在MySQL中创建100%的OQ输出:

  • 我的列名不包含公式:没有解决方法(也没有必要:您不应该将MySQL客户端用作表示层/格式层)
  • 对于所有非汇总列,汇总行将包含
    NULL
    。这可能是非常棘手的问题,可以用类似的方法来解决。IMHO这也归结为“不要使用MySQL客户端作为表示层”

试试这个

SELECT 
  IFNULL(Nameid,'Total'),
  IFNULL(Name, '------'),
  IFNULL(Product,'-----'),
  IFNULL(Place,'-----'),
  Amount, Price, Expence, Price_plus_Expence, Amount_minus_Price_plus_Expence
FROM (
  -- original answer query here
) AS baseview

如果您有多个价格和每个用户的金额,您可以这样做

     select t1.Nameid , t1.Name ,   t2.Product , t3.Place  , t1.amount , t2.Price  , t3.Expence ,t2.price + t3.expence  , t1.amount-(t2.price + t3.expence)
 from table1 t1
 inner join table2 t2 
 on t1.Nameid = t2.Nameid
 inner join table3 t3
 on t1.Nameid = t3.Nameid
 order by t1.Nameid

编辑

为了你的愿望,试试这个

    select t1.Nameid , t1.Name ,   t2.Product , t3.Place  , sum(t1.amount) sumamount, sum(t2.Price) sumprice , sum(t3.Expence) sumexpence,t2.price + t3.expence as priceexpence , t1.amount-(t2.price + t3.expence) as amountMinuspriceexpence
 from table1 t1
 inner join table2 t2 
 on t1.Nameid = t2.Nameid
 inner join table3 t3
 on t1.Nameid = t3.Nameid
 group by t1.Nameid
 order by t1.Nameid

我们都想要一些结果。这里的答案取决于你是否展示了你所做的尝试。。。我承认我以前从未见过使用rollup的
。美好的嗨,我编辑了我的问题,请检查并发布正确答案嗨,我编辑了我的问题,请检查并发布正确答案
     select t1.Nameid , t1.Name ,   t2.Product , t3.Place  , t1.amount , t2.Price  , t3.Expence ,t2.price + t3.expence  , t1.amount-(t2.price + t3.expence)
 from table1 t1
 inner join table2 t2 
 on t1.Nameid = t2.Nameid
 inner join table3 t3
 on t1.Nameid = t3.Nameid
 order by t1.Nameid
    select t1.Nameid , t1.Name ,   t2.Product , t3.Place  , sum(t1.amount) sumamount, sum(t2.Price) sumprice , sum(t3.Expence) sumexpence,t2.price + t3.expence as priceexpence , t1.amount-(t2.price + t3.expence) as amountMinuspriceexpence
 from table1 t1
 inner join table2 t2 
 on t1.Nameid = t2.Nameid
 inner join table3 t3
 on t1.Nameid = t3.Nameid
 group by t1.Nameid
 order by t1.Nameid
   select t1.Nameid , t1.Name ,   t2.Product , t3.Place  , t1.amount , t2.Price  , t3.Expence ,t2.price + t3.expence as priceexpence , t1.amount-(t2.price + t3.expence) as amountMinuspriceexpence
 from table1 t1
 inner join table2 t2 
 on t1.Nameid = t2.Nameid
 inner join table3 t3
 on t1.Nameid = t3.Nameid
 union all 

 select 'Toatal','--','--','--', sum(t1.amount)as sumamount,sum(t2.price) as sumprice,sum(t3.Expence)as sumexpence, sum(t2.price + t3.expence) as sumpriceexpence ,sum(t1.amount-(t2.price + t3.expence))sumamountminuspriceexpence
 from table1 t1
 inner join table2 t2 
 on t1.Nameid = t2.Nameid
 inner join table3 t3
 on t1.Nameid = t3.Nameid