具有相同特定列的列中的mysql汇总值

标签 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`

所以我需要什么......我需要下一个带有总价摘要的列,其中行是相同的类别

编辑

表格示例

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

最佳答案

我认为这应该可以解决您的问题:

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

关于具有相同特定列的列中的mysql汇总值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26212136/

相关文章:

PHP/MySQL 随机无法解析正确的日期时间

php - 在没有用户交互的情况下在特定时间后运行 sql 查询?

mysql - 如何在mysql中使用id设置用户名

mysql - 将主键值复制/更新到同一表的另一列

mysql - 如何找到下一个订单ID mysql

php - 警告:mysql_fetch_assoc():提供的参数不是第22行上/home/a5751969/public_html/func/album.func.php中的有效MySQL结果资源。

Mysql从一行中删除重复项并更新该行

PHP 如何在 url 中指定 "back one directory level"

mysql - CodeBlocks c++ 使用 SQLAPI++ 连接到 MySQL 时出错

php - 如何使用 ODBC 驱动程序从 MS Access 自动插入新记录到 MySql?