查询3天没有活动的用户

时间:2014-11-19 15:48:10

标签: mysql

我有一个系统,用户可以在其中发送彼此的评论。我想找到3天没有发表任何评论的用户。此外,这将在每日cron上运行,我只希望他们在最后一次评论后每3天收到一次电子邮件,所以我试图在3到4天之间查找他们的最新评论。

这就是我所拥有的:

$sql = "SELECT u.firstName, u.email 
        FROM user AS u
        LEFT JOIN comment AS c ON u.id=c.sender
        WHERE (DATE(c.date) BETWEEN ( CURDATE() - INTERVAL 4 DAY ) 
              AND ( CURDATE() - INTERVAL 3 DAY )) 
              AND u.disabled=0 
        GROUP BY u.id";

1 个答案:

答案 0 :(得分:0)

使用DATE_SUB从提供的日期中减去间隔

试试这个

$sql = "SELECT u.firstName, u.email 
        FROM user AS u
        LEFT JOIN comment AS c ON u.id=c.sender
        WHERE (DATE(c.date) BETWEEN DATE_SUB(NOW(),INTERVAL 4 DAY)
                 AND DATE_SUB(NOW(),INTERVAL 3 DAY)
               AND u.disabled=0 
        GROUP BY u.id"