gpt4 book ai didi

mysql - 显示自用户上次评论以来的天数

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:18 26 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 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

正确输出过去 3 天未发表评论的用户。我如何修改它以显示自他们上次评论以来的天数?

最佳答案

如果您需要自上次评论以来的天数,那么您将需要某种join:

select u.*, datediff(curdate(), last_commented)
from users u left join
comments c
on c.user_id = u.user_id
where u.status = 2
group by u.user_id
having max(last_commented) < curdate() - interval 3 day or
max(last_commented) is null;

此版本包括完全未发表评论的用户。

关于mysql - 显示自用户上次评论以来的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47136619/

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