2014-10-06 96 views
0

我有一個像下面MySQL的彙總值

 
+----+---------+-------+-----+----------+ 
| id | user_id | price | qty | category | 
+----+---------+-------+-----+----------+ 

,我有一個查詢數據庫中的表

 
SELECT *,(SELECT `login` FROM `users` WHERE `users`.`user_id`=`warehouse`.`user_id`) AS `login`, (`price`*`qty`) AS `total_price` FROM `warehouse` 

所以我需要什麼...我需要在下一個山坳總價格,其中行是同一類別的sumary

編輯

表樣品

 
CREATE TABLE IF NOT EXISTS `warehouse` (
    `id` int(11) NOT NULL DEFAULT 0, 
    `user_id` int(11) DEFAULT NULL, 
    `price` int(11) DEFAULT NULL, 
    `qty` int(11) DEFAULT NULL, 
    `category` int(11) DEFAULT NULL 
); 

INSERT INTO `warehouse` (`id`, `user_id`, `price`, `qty`, `category`) VALUES 
(1, 1, 10, 5, 1), 
(2, 1, 15, 2, 1), 
(3, 1, 8, 3, 1), 
(4, 1, 28, 10, 1), 
(5, 1, 10, 1, 2); 

CREATE TABLE IF NOT EXISTS `users` (
    `user_id` int(11) DEFAULT NULL, 
    `login` varchar(32) DEFAULT NULL 
); 

INSERT INTO `users` (`user_id`, `login`) VALUES 
(1, 'test'); 

結果與我的查詢

 
+----+---------+-------+-----+----------+-------+-------------+ 
| id | user_id | price | qty | category | login | total_price | 
+----+---------+-------+-----+----------+-------+-------------+ 
| 1 | 1  | 10 | 5 | 1  | test | 50   | 
+----+---------+-------+-----+----------+-------+-------------+ 
| 2 | 1  | 15 | 2 | 1  | test | 30   | 
+----+---------+-------+-----+----------+-------+-------------+ 
| 3 | 1  | 8  | 3 | 1  | test | 24   | 
+----+---------+-------+-----+----------+-------+-------------+ 
| 4 | 1  | 28 | 10 | 2  | test | 280   | 
+----+---------+-------+-----+----------+-------+-------------+ 
| 5 | 1  | 10 | 1 | 1  | test | 10   | 
+----+---------+-------+-----+----------+-------+-------------+ 

我需要什麼

 
+----+---------+-------+-----+----------+-------+-------------+--------------------------+ 
| id | user_id | price | qty | category | login | total_price | sum_category_total_price | 
+----+---------+-------+-----+----------+-------+-------------+--------------------------+ 
| 1 | 1  | 10 | 5 | 1  | test | 50   | 114      | 
+----+---------+-------+-----+----------+-------+-------------+--------------------------+ 
| 2 | 1  | 15 | 2 | 1  | test | 30   | 114      | 
+----+---------+-------+-----+----------+-------+-------------+--------------------------+ 
| 3 | 1  | 8  | 3 | 1  | test | 24   | 114      | 
+----+---------+-------+-----+----------+-------+-------------+--------------------------+ 
| 4 | 1  | 28 | 10 | 2  | test | 280   | 280      | 
+----+---------+-------+-----+----------+-------+-------------+--------------------------+ 
| 5 | 1  | 10 | 1 | 1  | test | 10   | 114      | 
+----+---------+-------+-----+----------+-------+-------------+--------------------------+ 
+0

爲您所標記的問題,那是不是'MySQL'語法。它看起來像'sql-server'。請更正標籤。 – Jens 2014-10-06 08:13:30

+0

我編輯過MySQL的語法,對不起 – 2014-10-06 08:46:28

+0

您能添加一些示例數據和預期結果嗎? – Jens 2014-10-06 08:48:37

回答

0

我認爲應該解決您的問題:

SELECT *,(SELECT `login` FROM `users` WHERE `users`.`user_id`=`warehouse`.`user_id`) AS `login`, (`price`*`qty`) AS `total_price` 
FROM warehouse join (select category, sum((`price`*`qty`)) AS `total_price` from warehouse group by category) as totals on warehouse.category=totals.category 
+0

完美,tnx ... – 2014-10-06 10:04:10

0
select *,(price*qty) AS total_price from Warehouse 
join (select category,sum(price*qty) as sum_category_total_price from Warehouse group by category) as Temp 
on Temp.category=Warehouse.category 
join users 
on users.user_id=warehouse.user_id 

DEMO

+0

這不是我所需要的,我需要total_price的摘要,其中是同一類別...在演示中是這些值50 + 200 = 250 – 2014-10-06 08:55:55