如何将行产品转换为列

时间:2017-11-24 11:14:02

标签: php mysql sql rows

我有3个表SQL

  • 产品[id,name_product]
  • customers [id,name_customer]
  • data_prodcuts [id,id_product,id_customer,quantity_data]

如何转换PHP和& MYSQL将表产品名称列添加到列中,并获取所有客户行的组id_product sum(quantity_data)。

谢谢..

我使用SQL

SELECT 
data_prodcuts.id_product, 
sum(data_prodcuts.quantity_data) as total,
data_prodcuts.id_customer,
products.name_product,
customers.name_customer
FROM data_prodcuts
LEFT JOIN products ON products.id = data_prodcuts.id_product
LEFT JOIN customers ON customers.id = data_prodcuts.id_customer
GROUP BY name_product, name_customer

0 个答案:

没有答案