我正面临这个(至少对我而言)挑战:
我有一个论坛,其中帖子存储在表 posts
中,评论存储在表 comments
中。发布帖子时,时间戳存储在列 INT(11) posts.created_at
中。与评论相同...当发表评论时,评论的时间戳将存储在 comments.created_at
中。
但是,现在我想知道出现第一条评论所需的平均时间(以秒为单位)。当然,如果没有给出任何评论,则不应将其计入平均值。
这可以在一个 SQL 查询中完成吗?如果不是,如何用最少的查询来完成?
希望你有想法,而我没有。
最佳答案
希望这对你有用:
select avg(first_comments.first_time - posts.created_at)
from posts
join (select comments.post_id
min(comments.created_at) as first_time
from comments
group by post_id) as first_comments
on first_comments.post_id = posts.id;
关于mysql - 计算两个 MySQL 表中发布和第一条评论之间的平均时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8573545/