MySQL查找已过期的付费记录

时间:2017-01-30 23:06:48

标签: mysql

我有以下两个数据成员表,其中包含名为member_id的自动ID的成员名称和存储过期日期的付费表以及两个表之间加入的member_id。

我试图通过选择2017年带有NOT EXIST条款的付费记录来查询将返回今年未续订的所有会员。此查询不会返回任何结果。

SELECT * 
  FROM members
     , paid
 WHERE members.member_id = paid.member_id
   AND paid.year = '2016-12-31'
   AND NOT EXISTS ( SELECT members.member_id 
                      FROM members
                         , paid
                     WHERE members.member_id = paid.member_id
                       AND paid.year = '2017-12-31')

我不确定我在这里做错了什么。

1 个答案:

答案 0 :(得分:1)

 SELECT * 
   FROM members
   JOIN paid
   ON members.member_id = paid.member_id
   WHERE paid.year = '2016-12-31'
   AND members.member_id NOT IN ( SELECT members.member_id 
                      FROM members
                      JOIN paid
                      ON members.member_id = paid.member_id
                      WHERE paid.year = '2017-12-31')