我正在尝试使用MySQL查询执行某些操作并请求您帮助解决此问题。
事实上,我想向每天在我的网站上收到通知的用户发送每日电子邮件(带有cron任务)。我已经有了以下查询:
SELECT u.username, u.email, COUNT(*) AS nb_notification
FROM notification n, user u
WHERE n.to_user_id = u.id
AND u.status = 1
AND n.date >= now() - INTERVAL 1 DAY
AND n.status = 0
GROUP BY u.username
现在,我想改进此查询,以便在同一时间内不会发送所有电子邮件。为此,我有以下想法:只获取在同一小时内注册执行cron任务的用户。
用户的注册日期作为DATETIME字段存储在名为“register_date”的用户表中。
例如,如果我有两个用户:
用户Sophia在上午08:52注册了相同的日期
谢谢!
答案 0 :(得分:1)
SELECT u.username, u.email, COUNT(*) AS nb_notification
FROM notification n, user u
WHERE n.to_user_id = u.id
AND u.status = 1
AND n.date >= DATE_SUB(NOW(),INTERVAL 1 HOUR)
AND n.status = 0
GROUP BY u.username
尝试这可能会有所帮助
答案 1 :(得分:0)
我自己找到了解决方案,这是我正在寻找的MySQL查询:
SELECT u.username, u.email, COUNT(*) AS nb_notification
FROM notification n, user u
WHERE n.to_user_id = u.id
AND u.status = 1
AND n.date >= now() - INTERVAL 1 DAY
AND n.status = 0
AND HOUR(u.register_date) = HOUR(NOW())
GROUP BY u.username
感谢您的帮助。