查找最近12个月+当月销售(我的日期格式是mysql中的字符串)

时间:2017-08-01 08:03:28

标签: mysql inner-join

SELECT pd.DC_Date, SUM(pd.Quantity-pd.Credit_Quantity) AS 'Sales' 
FROM past_year_data PD
INNER JOIN idash_tb2_product_list PL ON PD.Product_Name = PL.Product_Name
WHERE PD.Branch_Name = 'Delhi'
AND DATE_FORMAT(STR_TO_DATE(DC_Date,'%m'), '%M') = DATE_FORMAT(STR_TO_DATE('7/31/2017', '%m'), '%M') 

这就是我想要做的。

1 个答案:

答案 0 :(得分:0)

您可以将String转换为Date并使用BETWEENDATE_ADD功能进行比较,例如:

SELECT pd.DC_Date,sum(pd.Quantity-pd.Credit_Quantity) AS 'Sales' 
FROM past_year_data pd
JOIN idash_tb2_product_list PL ON PD.Product_Name=PL.Product_Name
WHERE pd.Branch_Name = 'Delhi' AND
STR_TO_DATE(DC_Date,'%m') BETWEEN DATE_ADD(NOW(), INTERVAL -12 MONTH) AND NOW();