我有一个产品表,包含以下字段
productid
name
price
catid
deleted (can be any one from 'y' and 'n')
并且有一个销售表
salesid
productid
salestime
paymentstatus (enum, can have any one value: COMPLETED, REMAINING, CANCELLED)
现在我需要以下格式的数据
catid productids totalproducts totalsales
其中
productids:这将列出所有产品ID(逗号分隔),其中deleted ='n'且属于特定类别
totalproducts:属于特定类别的产品,其中deleted ='n'
totalsales:sales,其中产品属于特定类别而paymentstatus ='COMPLETED'
任何帮助。
答案 0 :(得分:0)
使用此查询,您将获得删除产品的特定类别的总销售额。
您是否想要这个或者您想要所有类别的销售,无论产品的状态如何?
SELECT p.catid AS catid,
GROUP_CONCAT(DISTINCT p.productid SEPARATOR ',') AS productids,
COUNT(DISTINCT p.productid) AS totalproducts,
COUNT(s.salesid) AS totalsales
FROM products p
LEFT OUTER JOIN sales s
ON s.productid = p.productid
AND s.paymentstatus = 'COMPLETED'
WHERE p.deleted = 'n'
GROUP BY p.catid