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
答案 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中创建100%的OQ输出:
NULL
。这可能是非常hackily用类似的东西解决。恕我直言,这也归结为“不要使用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
答案 1 :(得分: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
EDIT。
为了你的希望,请尝试这个
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