嗨,我有一个查询,该查询将检索付款方式类型为cash且memberstatustype的id为2,3,6的成员。当成员的付款方式类型为Cash,dd和card时,该查询工作正常,但是它将检索没有paymethod的成员的所有详细信息为card,这是我的查询

SELECT members.member_Id, members.member_Lastname, members.member_Firstname, members.member_PostCode,members.member_Reference,  members.member_Dob,30*memberToMship_ChargePerPeriod/DateDiff(memberToMship_EndDate, memberToMship_StartDate) As monthly_amount, mshiptypes.mshipType_Name, mshipstatustypes.mshipStatusType_Name,membertomships.memberToMship_EndDate,IF(mshipOption_Period='year', TIMESTAMPDIFF (YEAR,memberToMship_StartDate, memberToMship_EndDate),  TIMESTAMPDIFF (MONTH ,memberToMship_StartDate,memberToMship_EndDate ) ) *memberToMship_ChargePerPeriod As Total
FROM members
LEFT JOIN membertomships ON membertomships.member_Id = members.member_Id
LEFT JOIN mshipstatustypes ON mshipstatustypes.mshipStatusType_Id = membertomships.mshipStatusType_Id
LEFT JOIN mshipoptions ON mshipoptions.mshipOption_Id = membertomships.mshipOption_Id
LEFT JOIN mshiptypes ON mshiptypes.mshipType_Id = mshipoptions.mshipType_Id
WHERE
    membertomships.memberToMship_PayMethod='Card' OR  mshipstatustypes.mshipStatusType_Id='2' OR  mshipstatustypes.mshipStatusType_Id = '3'  OR mshipstatustypes.mshipStatusType_Id='6';


谁能在这方面帮助我....
我在“或”条件下做错了什么吗

最佳答案

您的WHERE子句需要如下所示:

WHERE membertomships.memberToMship_PayMethod='Card'
AND (mshipstatustypes.mshipStatusType_Id='2'
OR  mshipstatustypes.mshipStatusType_Id = '3'
OR mshipstatustypes.mshipStatusType_Id='6');


您的原始查询将查找具有以下任一者的用户:


membertomships.memberToMship_PayMethod ='卡'
mshipstatustypes.mshipStatusType_Id ='2'
mshipstatustypes.mshipStatusType_Id ='3'
mshipstatustypes.mshipStatusType_Id ='6'


您想要的是拥有


membertomships.memberToMship_PayMethod ='卡'


AND以下之一:


mshipstatustypes.mshipStatusType_Id ='2'
mshipstatustypes.mshipStatusType_Id ='3'
mshipstatustypes.mshipStatusType_Id ='6'

10-07 17:14