0

我有以下 mysql 表。

1. members
----------
|id |name|
----------
|001|aaa |
|002|bbb |
|003|ccc |
|004|ddd |
----------

2. membershipfees
----------------
|id |date      |
----------------
|001|2012-01-01|
|002|2012-01-01|
----------------

我想显示当月未支付费用的成员列表。我的php代码是,

SELECT Members.id, Members.Name FROM Members
    LEFT JOIN MembershipFees
    ON Members.id = MembersipFees.id
    WHERE DATE_FORMAT(MembershipFees.Date, '%Y%m') <> DATE_FORMAT(NOW(), '%Y%m')
    ORDER BY Members.id ASC

但它显示 0 行。请有任何建议。

4

4 回答 4

3

你需要把MembersipFees.id IS NULL条件是 where 子句。此外,如果date字段是 varchar 列,那么您应该使用STR_TO_DATE函数。

SELECT Members.id, Members.Name
FROM Members
     LEFT JOIN membershipfees
        ON Members.id = membershipfees.id
           AND DATE_FORMAT(STR_TO_DATE(membershipfees.`Date`, '%d-%m-%Y'), '%Y%m') = 
                           DATE_FORMAT(NOW(), '%Y%m')
WHERE membershipfees.id IS NULL
ORDER BY Members.id ASC;

在此处查看 SQLFIDDLE 演示

于 2012-08-22T06:29:25.313 回答
1

我现在无法测试它,但请尝试让我知道它是否适合您:

SELECT m.id, m.Name FROM Members m
Where not exists 
    (select 1 from membershipfees mm 
     where 
     mm.Date > DATE_FORMAT(NOW(), '%Y-%m')
     and mm.id = m.id)

更新:
它对我有用

于 2012-08-22T06:32:11.137 回答
0

试试这个:

您只需在查询中添加membershipfees.id IS NULL条件

SELECT members.id, members.Name  
FROM   members 
LEFT outer JOIN membershipfees 
ON       members.id = membershipfees.id 
and    DATE_FORMAT(membershipfees.Date, '%Y%m')
                          <> DATE_FORMAT(NOW(), '%Y%m') 
where  membershipfees.id IS NULL
ORDER BY members.id ASC
于 2012-08-22T06:33:31.757 回答
0

试试这个查询..

SELECT Members.id, Members.Name FROM Members where id not in (select id from membershipfees WHERE DATE_FORMAT(MembershipFees.Date, '%Y%m') <> DATE_FORMAT(NOW(), '%Y%m') ORDER BY Members.id ASC)
于 2012-08-22T07:11:22.363 回答