我想从月份到月份之间从下表中得到总价格的总和。例如:1月到3月如何获取月份的名称以及特定月份总和的总和。我完全对此感到困惑。任何人请帮助我。我将输入像'04 -2014'传递给'05 -2014'
促销表
id_sale date time
1 2014-05-05 12.30 am
2 2014-05-06 10.30 am
3 2014-05-25 12.30 am
促销产品表
id_sale_product id_sale price quantity id_product
1 1 10.00 1 1
2 1 20.00 1 2
3 2 20.00 3 5
4 3 20.00 4 6
我想按销售表中的日期过滤并获得每个日期的价格*数量=总和的总和,即2014-05-05,2014-05-06..etc
我试过下面的查询
$query = 'SELECT sp.`id_product_type`,sp.`id_product`,sp.`quantity`,sp.`price`,s.`date`,SUM(sp.`price` * sp.`quantity`) AS total
FROM `'._DB_PREFIX_.'sale_product` sp
LEFT JOIN '._DB_PREFIX_.'sales s ON (sp.id_sale = s.id_sale)
WHERE DATE_FORMAT(s.`date`, "%m-%Y") BETWEEN "'.$monthFrom.'" AND "'.$monthTo.'"';
输出应为:
Example: Month
January Rs. 2,00,000
Feburary Rs. 2,20,000
答案 0 :(得分:0)
此查询应该有所帮助:
SELECT DATE_FORMAT(s.date, "%M-%Y"),SUM(sp.`price` * sp.`quantity`) AS total
FROM `'._DB_PREFIX_.'sale_product` sp
LEFT JOIN '._DB_PREFIX_.'sales s ON (sp.id_sale = s.id_sale)
WHERE DATE_FORMAT(s.`date`, "%m-%Y") BETWEEN "'.$monthFrom.'" AND "'.$monthTo.'"';
GROUP BY DATE_FORMAT(s.date, "%M-%Y")