无论条件如何,检索所有细节

时间:2011-07-11 11:27:08

标签: mysql sql

您好我有一个查询,它将检索付款方式类型为现金且会员类型为id为2,3,6的会员。当成员有付费方式类型的查询工作正常是现金和DVD和卡但它将检索没有paymethod卡的成员的所有细节,这是我的查询

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';

任何人都可以帮助我.... 我在“OR条件”中做错了什么

3 个答案:

答案 0 :(得分:1)

您的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'

答案 1 :(得分:1)

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' 
  AND  mshipstatustypes.mshipStatusType_Id in ('2', '3', '6');

答案 2 :(得分:0)

你也可以使用这个运行得更快的where子句

WHERE membertomships.memberToMship_PayMethod='Card' 
AND mshipstatustypes.mshipStatusType_Id in('2','3','6')