试图获得卡和现金成员

时间:2011-08-01 11:35:27

标签: mysql sql left-join inner-join

您好我这样做是为了获取付款方式类型现金的会员详情

       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='Cash'
          AND members.member_Active LIKE 'y%'
          AND (mshipstatustypes.mshipStatusType_Id='2' 
            OR mshipstatustypes.mshipStatusType_Id = '3' 
            OR mshipstatustypes.mshipStatusType_Id='6')"

和另一个类似的查询,用于通过付款方式类型卡获取会员详细信息

                               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 members.member_Active LIKE 'y%'
        AND (mshipstatustypes.mshipStatusType_Id='2' OR  mshipstatustypes.mshipStatusType_Id = '3' 
            OR mshipstatustypes.mshipStatusType_Id='6')"

但我希望会员详细信息那些有付款方式的人是“现金和卡”(两种类型成员)

对此查询有任何帮助...

我正在使用MySQL

非常感谢........

修改后的查询

                       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
         INNER JOIN membertomships ON membertomships.member_Id = members.member_Id
        INNER JOIN mshipstatustypes ON mshipstatustypes.mshipStatusType_Id = membertomships.mshipStatusType_Id
        INNER JOIN mshipoptions ON mshipoptions.mshipOption_Id = membertomships.mshipOption_Id
        INNER JOIN mshiptypes ON mshiptypes.mshipType_Id = mshipoptions.mshipType_Id
        WHERE membertomships.memberToMship_PayMethod IN ('Card','Card','Cheque')
        AND members.member_Active LIKE 'y%'
        AND ( mshipstatustypes.mshipStatusType_Id='2' OR  mshipstatustypes.mshipStatusType_Id = '3'
          OR mshipstatustypes.mshipStatusType_Id='6');

但它没有显示正确的值..

嗨但是有一个问题我有六个卡会员和3个现金会员,它们分别显示6个和3个但是当合并时(卡和现金)它只显示4个。但它必须显示(6 + 3)我不知道怎么会这样,你会告诉任何解决方案。

2 个答案:

答案 0 :(得分:4)

假设唯一的付款方式是现金或卡,您可以省略where membertomships.memberToMship_PayMethod='Card'

如果还有其他付款方式,您可以将其更改为

membertomships.memberToMship_PayMethod in ('Card', 'Cash')

答案 1 :(得分:1)

使用OR或IN

WHERE membertomships.memberToMship_PayMethod IN ('Card','Cash')

WHERE (membertomships.memberToMship_PayMethod ='Card' OR membertomships.memberToMship_PayMethod ='Cash')