0

我有以下问题。

人们在网站上发布任务。

人们发布的所有任务都进入任务表,结构如下:

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

但我认为这不是正确的方法。我应该怎么办?

4

2 回答 2

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;

未经测试,但你明白了。

于 2012-07-26T16:03:05.530 回答
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
于 2015-11-26T15:30:46.030 回答