gpt4 book ai didi

mysql - 仅显示3天内未添加评论的用户的ID

转载 作者:行者123 更新时间:2023-11-29 10:32:31 25 4
gpt4 key购买 nike

我有两个多行表。第一个称为 comments 并具有以下结构:

user_id   last_commented
........................
9239289 2017-11-06
4239245 2017-11-05
4239245 2017-11-03
6239223 2017-11-02
1123139 2017-11-04

第二个称为users,具有以下结构:

user_id   user_name   user_status
.................................
9239289 First Name 0
4239245 First Name2 2
6239223 First Name3 1
1123139 First Name4 2

我需要一个查询来显示过去 3 天内未添加评论且 user_status 等于 2 的用户。

这是我到目前为止的查询:

SELECT user_name FROM
(
SELECT MAX(last_commented) lastdate,user_id
FROM `comments`
GROUP BY user_id
) A
LEFT JOIN users
USING (user_id)
WHERE lastdate < ( DATE(NOW()) - INTERVAL 3 DAY )
AND user_status = 2

最佳答案

例如,您可以使用NOT EXISTS

select u.*
from users u
where not exists (
select 1
from comments c
where c.user_id = u.user_id and last_commented > DATE(NOW()) - INTERVAL 3 DAY
) and user_status = 2

编辑:如果您想在结果中显示自上次用户登录以来的天数,则可以使用以下方法:

select u.*, datediff(DATE(now()), comment.last_commented)
from users u
join (
select user_id, max(last_commented) last_commented
from comments
group by user_id
) comment on comment.user_id = u.user_id
where last_commented <= DATE(NOW()) - INTERVAL 3 DAY

这种方法使用子查询来查找最后评论日期,然后将其用于过滤和计算。

关于mysql - 仅显示3天内未添加评论的用户的ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47134815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com