我有2张桌子:warehouse ['id','warehouse_name']
和products ['id','product_name','qty','warehouse_id']
通过warehouse_id
连接的。
我需要groupBy product_name和求和列数量和仓库
例如:Product 2 14 WH1,WH2
最佳答案
调查您的样本输出数据,我认为您正在寻找SUM
和GROUP_CONCAT
SELECT p.product_name,
sum(p.qty) AS qty,
GROUP_CONCAT(w.warehouse_name
ORDER BY w.warehouse_name ASC SEPARATOR ',') AS warehouse
FROM product p
INNER JOIN warehouse w ON p.warehouse_id = w.id
GROUP BY p.product_name;
结果:
product_name qty warehouse
------------ --- ---------
product 2 14 WH1,WH2
product 3 2 WH1
product 5 4 WH3
DEMO
关于mysql - Yii2获取列的总和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46862701/