两个日期之间的mySQL查询问题与sum

时间:2015-12-10 17:13:02

标签: mysql

我有下一张名为expense_partial_payment的表。

enter image description here

我的查询是下一个,我需要将amount_paid的总和作为total_paid:

SELECT id_partial_payment, epp.id_billing, epp.id_user, epp.month_of_payment,  
            COALESCE(SUM(epp.amount_paid),0) AS total_paid,
            (be.total - amount_paid) AS pending_total , be.total as total

            FROM expenses_partial_payment epp 
            INNER JOIN BillingExpenses be  ON epp.id_billing = be.id_billing
            WHERE epp.id_user = 23 

enter image description here

但是当我在

中加入子句时
WHERE epp.id_user = 23 AND  (Month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01'))

enter image description here

我有一个total_piad错误,查询只返回最后一行(300)并且没有求和amount_paid,正确的总和必须是501.00

我该怎么做才能获得正确的总和,添加子句来过滤month_of_payment

  

(month_of_payment BETWEEN'2015-01-01'和LAST_DAY('2016-01-01'))

2 个答案:

答案 0 :(得分:0)

month_of_payment BETWEEN' 2015-01-01'和LAST_DAY(' 2016-01-01')

last day()返回月份的最后一天而不是年份。所以它不会在一个月内提取数据。

答案 1 :(得分:0)

尝试用此

替换您的日期
STR_TO_DATE('01/01/2015', '%m/%d/%Y')