我有一个脚本,根据他们在我们俱乐部在线商店购买会员资格的最后日期,向会员资格即将到期的会员发送自动续订通知:
SELECT members.id, first, last, class, email, name AS membershipType, lifetime, store_transactions.dateModified FROM store_transactions
LEFT JOIN members ON memberID = members.id
WHERE
(
DATE(store_transactions.dateModified) = SUBDATE(CURDATE(), INTERVAL 1 YEAR)
/*AND
NOT EXISTS(SELECT * FROM store_transactions WHERE DATE(store_transactions.dateModified) > SUBDATE(CURDATE(), INTERVAL 1 YEAR))*/
)
AND categoryID = 1 AND lifetime != 'Y'
GROUP BY members.id
ORDER BY last, first
我每天通过 CRON 作业运行这个脚本,并且通过向一年前购买会员资格的所有成员发送电子邮件,它运行良好(或者我认为是这样)。但是,我没有考虑到会员可以在我的脚本发送自动提醒之前更新他们的会员资格)。
**更新:在尝试实施你们中的几个人推荐的解决方案时(谢谢!),这是我目前的查询:
SELECT members.id, first, last, class, email, name AS membershipType, lifetime, store_transactions.dateModified, categoryID FROM store_transactions
LEFT JOIN members ON memberID = members.id
GROUP BY members.id
HAVING categoryID = 1 AND lifetime != 'Y'
AND DATE(MAX(store_transactions.dateModified)) = '2012-03-24' /* specific date to simplify debugging */
ORDER BY last, first
我觉得这非常接近正确的解决方案,但是由于我的 store_transactions 表具有不同类别的事务,可以由同一个成员(即会员、活动门票、广告、捐赠等)。出于此查询的目的,我只对查看成员类别事务(“categoryID = 1”)感兴趣。附加的“lifetime != 'Y'”禁止此查询返回终身成员,其成员资格永不过期。
总而言之,我希望此查询返回恰好在一年前具有“categoryID = 1”事务的成员的记录,除非他们有“categoryID = 1”的更新事务。这是我的 store_transactions 表中的字段,以防万一:
id invoice memberID categoryID productID name price 数量 dateModified addedBy