如何根据其变体,variant_options及其来自不同表的sku对表进行分组
我的查询是:
SELECT p.id AS pId, p.name AS pName,
v.name AS vName,
vo.name AS voName,
s.sku, s.price, s.qty
FROM products p
LEFT JOIN variants v ON v.product_id = p.id
LEFT JOIN variant_options vo ON vo.variant_id = v.id
LEFT JOIN skus s ON s.product_id = p.id
它像这样显示
table:
pId | pName | vName | voName | sku | price | qty
1 | adidas | size | 42 | AD42 | 100 | 10
1 | adidas | size | 43 | AD42 | 100 | 10
1 | adidas | size | 42 | AD43 | 100 | 10
1 | adidas | size | 43 | AD43 | 100 | 10
我希望我的桌子成为:
table:
pId | pName | vName | voName | sku | price | qty
1 | adidas | size | 42 | AD42 | 100 | 10
| 43 | AD43 | 100 | 10
答案 0 :(得分:0)
根据需要添加GROUP BY。
SELECT p.id AS pId, p.name AS pName,
v.name AS vName,
vo.name AS voName,
s.sku, s.price, s.qty
FROM products p
LEFT JOIN variants v ON v.product_id = p.id
LEFT JOIN variant_options vo ON vo.variant_id = v.id
LEFT JOIN skus s ON s.product_id = p.id
GROUP BY v.name, vo.name, s.product_id
但是有时您可能只选择其中一个GROUP BY以减小粒度。例如:
GROUP BY s.product_id