试图在到期日之前找到逾期的会员

时间:2011-07-11 16:35:08

标签: mysql sql mysql-error-1111

这是实际查询

Select members.member_Id, membertomships.memberToMship_DueDay, sum(memberToMship_ChargePerPeriod)-sum(memberAccTran_Value) as StillDue 
from membertomships 
left join mshipoptions on membertomships.mshipOption_Id = mshipoptions.mshipOption_Id
left join members on membertomships.member_Id = members.member_Id
left join memberacctrans on memberacctrans.member_Id = members.member_Id
WHERE sum(if(memberToMship_DueDay<CURDATE(),0,memberToMship_ChargePerPeriod))>sum(memberAccTran_Value);

修改后的查询:

Select 
    members.member_Id, 
    membertomships.memberToMship_DueDay, 
    sum(sum(memberToMship_InductionFee+memberToMship_JoinFee + (IF(mshipOption_Period='year', TIMESTAMPDIFF (YEAR ,memberToMship_StartDate, memberToMship_EndDate), TIMESTAMPDIFF (MONTH ,memberToMship_StartDate, memberToMship_EndDate)) * memberToMship_ChargePerPeriod))) - sum(memberAccTran_Value) as StillDue 
from membertomships 
left join mshipoptions on membertomships.mshipOption_Id = mshipoptions.mshipOption_Id
left join members on membertomships.member_Id = members.member_Id
left join memberacctrans on memberacctrans.member_Id = members.member_Id
group by member_Id
having sum(if(memberToMship_DueDay<today(),0,memberToMship_ChargePerPeriod))>sum(memberAccTran_Value);

注意:paymenttable = memberacctrans table

得到了这样的错误

                          Error Code: 1111
                         Invalid use of group function

任何人都可以帮我解决这个问题

2 个答案:

答案 0 :(得分:1)

如果你愿意,你会让事情变得更容易 每笔付款还包括它所指的哪个组织记录!

尝试以下SQL查询以显示所有未结会费:

Select member_id, max(membertomship_dueday), sum(membertomship_Totalfee)-sum(payment_Money) as StillDue
from membertomship 
left join member_table on membertomship.memberID = member_table.member_id
left join payments on payment_member_id = member_table.member_id
group by member_id
having sum(membertomship_Totalfee)>sum(payment_Money)

从那里你可以改进它,只计算已到期的费用: (我不知道您使用的是哪个数据库服务器,因此IF的语法可能是 不正确!)

Select member_id, (membertomship_dueday), sum(membertomship_Totalfee)-sum(payment_Money) as StillDue 
from membertomship 
left join member_table on membertomship.memberID = member_table.member_id
left join payments on payment_member_id = member_table.member_id
group by member_id
having sum(if(membertomship_dueday<today(),0,membertomship_Totalfee))>sum(payment_Money)

答案 1 :(得分:1)

不知道表中的数据类型并假设您使用简单的“日期”类型作为日期字段,您可以这样做:(注意您在哪里看到AS这是一个创建的字段,您可以重命名它们)< / p>

SELECT
member_overdue.member_id,
member_overdue.member_firstname,
member_overdue.member_lastname,
DATEDIFF(CURDATE(),membertomship.membertomship_dueday) AS days_diff
FROM
membertomship
Inner Join member_table AS member_overdue ON member_overdue.member_id = membertomship.member_Id
WHERE
membertomship.membertomship_dueday <  CURDATE()

现在在member_table中使用此数据集:

member_id    member_firstname    member_lastname
1            brandon             s
2            sally               s
3            gregg               s

和membertomship的这个数据集:

membertomship_id    membertomship_StartDate membertomship_enddate   membertomship_Totalfee  membertomship_dueday    membertomship_paymethod member_Id
1                   6/1/2011                7/1/2011                45                  7/1/2011                cash                      1
2                   7/1/2010                8/3/2011                45                  8/3/2011                cc                        2
3                   1/1/2009                5/1/2011                45                  5/1/2011                cc                        3

给我这个结果集:

member_id    member_firstname    member_lastname    days_diff
1            brandon             s                  10
3            gregg               s                  71

请记住,select语句member_overdue是我在内连接中设置的别名,左边和右边不在这里,只要付款也更新了membertomship表,你的另一个表就无关紧要了。如果这是真的,那么这个简单的查询就是您所需要的。