我有两张桌子:
答案 0 :(得分:0)
查看您的示例输出数据,我认为您正在寻找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 强>