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');

我希望输出应该是这样的:

Nameid|name|product|place|amount|price|expence|price + expence|amount - (price +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
4

2 回答 2

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 中创建 100% 的 OQ 输出:

  • 列名我不包含公式:不可能的解决方法(也没有必要:您不应该使用 MySQL 客户端作为表示/格式化层)
  • Rollup 行将包含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
于 2013-07-20T10:37:40.180 回答
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

在这里演示

如果每个用户有多个价格和数量,您可以这样做

    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

在这里演示

于 2013-07-20T10:39:15.683 回答