使用mysql查询计算直到提及日期的总金额

时间:2011-07-06 09:12:33

标签: mysql sql datetime date min

                   membership table
                         membership start date 2011-01-10
                         membership end date 2012-09-08
                         membership monthly amount £120.00
                         member_Id

                    member table
                       member_id

会员将在每个月的10号付款...我可以获得会员在2011-05-15这个日期支付的总金额,是否可以使用mysql查询。

这是sql查询 你们建议

                    SELECT TIMESTAMPDIFF(MONTH, membertomships.memberToMship_StartDate, MIN(membertomships.memberToMship_EndDate,'2011-7-06'))* memberToMship_ChargePerPeriod FROM membertomships WHERE membertomships.member_Id = '1';

但是在memberToMship_EndDate,'2011-7-06'在日期memberToMship_EndDate和此日期'2011-7-06'

之间发生错误

你会解释为什么它会给出错误

3 个答案:

答案 0 :(得分:1)

SELECT TIMESTAMPDIFF(MONTH, start_date, MIN(end_date,'2011-05-15')) * amount from membership where membershipId=@memberdshipId

包括当前日期也.......

 SELECT TIMESTAMPDIFF(MONTH, start_date, MIN(end_date,'2011-05-15')) * amount ,TIMESTAMPDIFF(MONTH, start_date, MIN(end_date,CURDATE())) * amount from membership where membershipId=@memberdshipId

这对我的ms sqlserver工作正常,请更改为mysql syntex

select datediff(Month,startdate,enddate)*amount as Enddateamount,
datediff(Month,startdate,'12/6/2010')*amount as amountongivendate,
datediff(Month,startdate,getdate())*amount as amounttoday
 from membership

sql server screen shot

答案 1 :(得分:0)

您的问题不是很清楚,我不认为您已正确解释了架构。如果你想得到每个人已支付的所有款项:

select sum(amount) from membership group by member_id

如果您希望按日期或member_id(s)限制,请随意添加where子句:

http://dev.mysql.com/doc/refman/5.0/en/select.html

答案 2 :(得分:0)

我猜你只有订阅数据不是所有的支付表,对吗? 那你需要这样的陈述

SELECT TIMESTAMPDIFF(MONTH, '2011-01-10', MIN('2012-09-08', '2011-05-15')) * 120.00

当然你应该用列名和参数替换常量。

这当然假设付款日10取决于订阅开始日期并且客户已按时付款。 :)