我有这样的表:
id | customer_id | location_id | brand_id | quantity 1 123 1 1 3 2 124 2 1 1 3 125 1 2 2 4 126 2 2 1 5 127 1 2 1 6 128 2 2 1
我想要这样的结果: 按location_id和每个brand_id数量总计分组
id | customer_id | location_id | brand_id | quantity 1 123 1 1 3 3 125 1 2 2 5 127 1 2 1 ------------------------------------------------------- total 1 3 2 3 2 124 2 1 1 4 126 2 2 1 6 128 2 2 1 ------------------------------------------------------- total 1 1 2 2
答案 0 :(得分:0)
您正在寻找的是SUM()
MySQL中的功能
SELECT SUM(THE-COLUMN-YOU-WANT-TO-SUM)
FROM TABLE_NAME;
让我们试试你的表,
SELECT SUM(quantity)
FROM orders;
这将为9
quantity
其SUMMATION
如果要获取特定名称的数据,可以像这样使用AS,
SELECT SUM(quantity) AS NEW_NAME
FROM orders;
好了,现在你要总结一个特定的列,只需使用WHERE
SELECT SUM(quantity) AS newquantity
FROM orders
WHERE location_id=1 AND brand_id=1;
希望这有帮助, 问候。