2013-07-20 102 views
0
CREATE TABLE `table1` (
`Nameid` int(6) NOT NULL, 
    `name` varchar(20) default NULL, 
    `amount` double default NULL, 
    PRIMARY KEY (`Nameid`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 


INSERT INTO `table1` (`Nameid`, `name`, `amount`) VALUES 
(1, 'chan', 2000), 
(2, 'john', 3000), 
(3, 'james', 2000); 

CREATE TABLE `table2` (
`Pid` int(5) NOT NULL auto_increment, 
`Nameid` int(5) default NULL, 
`product` varchar(20) default NULL, 
`price` double default NULL, 
PRIMARY KEY (`Pid`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; 


INSERT INTO `table2` (`Pid`, `Nameid`, `product`, `price`) VALUES 
(1, 3, 'ghee', 400), 
(2, 2, 'dhal', 100), 
(3, 1, 'chenna', 150); 

CREATE TABLE `table3` (
`Sid` int(5) NOT NULL, 
`Nameid` int(5) default NULL, 
`expence` double default NULL, 
`place` varchar(25) default NULL, 
PRIMARY KEY (`Sid`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 


INSERT INTO `table3` (`Sid`, `Nameid`, `expence`, `place`) VALUES 
(1, 2, 280, 'Ny'), 
(2, 1, 500, 'At'), 
(3, 3, 600, 'ca'); 

我想输出应该是这个样子的总和:怎么弄3分表列

填充NameID |名称|产品|地方|量|价格| expence |价格+ expence |量 - (价格+ expence )|

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 
+0

我们都想要一些结果。这里的答案取决于你是否显示了你的尝试。 –

回答

1

可以使用盲SUM()WITH ROLLUP来实现这一目标:

SELECT 
    table1.Nameid AS Nameid, 
    table1.name AS Name, 
    table2.product AS Product, 
    table3.Place AS Place, 
    SUM(table1.amount) AS Amount, 
    SUM(table2.price) AS Price, 
    SUM(table3.expence) AS Expence, 
    SUM(table2.price+table3.expence) AS Price_plus_Expence, 
    SUM(table1.amount-(table2.price+table3.expence)) AS Amount_minus_Price_plus_Expence 
FROM 
    table1 
    INNER JOIN table2 ON table2.Namid=table1.Nameid 
    INNER JOIN table3 ON table3.Namid=table1.Nameid 
GROUP BY table1.Nameid 
WITH ROLLUP 

编辑

有没有办法在MySQL创建OQ输出的100%:

  • 列名不包含公式:没有可能的解决方法(也没有必要:您不应将MySQL客户端用作演示文稿/格式层)
  • 汇总行将包含所有非汇总列的NULL。这可能很难用诸如此类的东西解决。恕我直言,这也归结为“不要使用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 
+0

+1圣牛......我承认我以前从来没有见过“汇总”。太好了! – Bohemian

+0

嗨,我已编辑我的问题,请检查并发布正确的答案 – user2601972

0

试试这个

 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 

DEMO HERE

如果你有多个价格和每用户量,你可以做到这一点

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 

DEMO HERE

编辑。

您希望ressult试试这个

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 

DEMO HERE

+0

嗨,我已编辑我的问题,请检查并发布正确的答案 – user2601972

+0

@ user2601972现在检查我编辑的答案 –