我有以下问题。
人们在网站上发布任务。
人们发布的所有任务都按照以下结构进入任务表:
id
user_id
date_posted
并且有一个包含下表的用户表
id
first_name
email
我需要收到超过一周未发布任务的人的电子邮件吗?我怎样才能做到这一点?我尝试了以下方法:
SELECT distinct u.id, u.email FROM user as u WHERE id IN (SELECT user_id FROM task WHERE paid = 1 AND DATE_ADD(CURDATE(), INTERVAL -7 DAY) <= `date_posted` AND `date_posted` < DATE_ADD(CURDATE(), INTERVAL -6 DAY)) AND role = 0
但我不认为这是正确的方法。我该怎么办?
答案 0 :(得分:0)
尝试此查询
SELECT u.id, u.email
FROM user as u WHERE id NOT IN ( /* select users who posted */
SELECT distinct user_id FROM task
WHERE paid = 1 AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= `date_posted`
) AND role = 0;
未经测试,但您明白了。
答案 1 :(得分:0)
试试这个
SELECT DISTINCT u.id, u.email
FROM (
SELECT t.user_id AS user_id
FROM task t
WHERE DATE(t.date_posted) > DATE_SUB(CURDATE(), INTERVAL 7 DAY)
) AS t
RIGHT JOIN USER u ON u.id = t.user_id
WHERE t.user_id IS NULL