2017-08-12 75 views
0

我有一個查詢來獲得過去六個月的日薪平均值,但我也想選擇一個月...... 例如,如果當前月份是八月然後六個月前是三月。知道哪個月是6個月前,Mysql

這是查詢我:

SELECT 
/*Month here*/ 
(round((sum(TotalReceived)/6)/26)) as 'Daily Salary Average' 
FROM SalaryPaymentReceipt 
WHERE PaymentDate BETWEEN CURDATE() - INTERVAL 6 MONTH AND CURDATE() 
AND FK_Employee=12345; 

在此先感謝。

+0

在BETWEEN定義,有左,右手邊。您將使用哪一方作爲SELECT字段來獲得所需的結果? – marekful

+0

您是從今天開始準確的6個月還是要包括6個月前的整個月? 它們是不同的要求。 – Jeremy

回答

0

我得到了自己的答案

這是它

SELECT 
Month(subdate(curdate(),interval 6 month)) as 'Month', 
(round((sum(TotalReceived)/6)/26)) as 'Daily Salary Average' 
FROM SalaryPaymentReceipt 
WHERE PaymentDate BETWEEN CURDATE() - INTERVAL 6 MONTH AND CURDATE() 
AND FK_Employee=12345;