2015-06-06 55 views
0

我想表明的總和來(ID = 37)本月總和()的MySQL - PHP

我的代碼:

SELECT SUM(Total) 
FROM invoices 
WHERE user_id = 37 
And ModifiedTime BETWEEN DATE_SUB(CURDATE(),INTERVAL (DAY(CURDATE())-1) DAY) 
AND LAST_DAY(NOW()) 

結果= NULL

回答

0

你可以試試這個:

SELECT SUM(Total) 
FROM invoices 
WHERE user_id=37 
    And MONTH(ModifiedTime) = MONTH(now()) 
0

請嘗試此查詢: -

SELECT SUM(Total) 
FROM invoices 
WHERE user_id = 37 
And ModifiedTime BETWEEN "'.date('Y-m-01 00:00:00').'" AND "'.date('Y-m-t').' 23:59:59" 

這會給你唯一的當月記錄總和。 日期('Y-m-01 00:00:00')給出當前月份的第一天。 date('Y-m-t')給出當天的日期。

0

你應該寫:

SELECT SUM(Total) 
FROM invoices 
WHERE user_id='37' 
    And ModifiedTime BETWEEN DATE_SUB(CURDATE(),INTERVAL (DAY(CURDATE())-1) DAY) AND LAST_DAY(NOW()) 

請參閱用戶ID已經由單引號包裹。

如果你還沒有得到結果,你應該顯示你的數據和表結構(也許是一個小提琴?)