我有2个表(在user_id的基础上加入);
1 = Task ( id, user_id,task_title,created_datetime)
2 = users (id,name)
我希望收到所有最近5天未记录时间的用户电子邮件。 预先感谢。
答案 0 :(得分:1)
您可以按以下方式使用子查询-
SELECT * FROM users
WHERE ID IN (
SELECT A.id
FROM users A
INNER JOIN Task B ON A.id = B.user_id
GROUP BY A.id
HAVING MAX(created_datetime) < DATE_ADD(NOW(), INTERVAL -5 DAY)
)
答案 1 :(得分:0)
如果您的表定义是这样的: 1 =任务(id,user_id,task_title,created_datetime) 2 =用户(ID,名称,电子邮件) 并且您正在使用created_datetime来存储登录时间,那么以下查询应该可以工作:
SELECT email FROM users
WHERE id not IN (
SELECT distinct A.id
FROM users A
INNER JOIN Task B ON A.id = B.user_id
where A.created_datetime > DATE_ADD(NOW(), INTERVAL -5 DAY)
)
答案 2 :(得分:0)
您可以尝试以下操作:
SELECT a.*
FROM users a
INNER JOIN
(SELECT *
FROM (SELECT *
FROM task
ORDER BY created_datetime DESC) AS a
GROUP BY a.user_id ) AS b
ON a.id = b.user_id
WHERE b.created_datetime < CURRENT_DATE - interval 5 day;