Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 返回WHERE子句限制的内部联接数的msyql查询_Mysql_Sql - Fatal编程技术网

Mysql 返回WHERE子句限制的内部联接数的msyql查询

Mysql 返回WHERE子句限制的内部联接数的msyql查询,mysql,sql,Mysql,Sql,我有下面的查询,试图计算出一篇帖子发表后30分钟内的评论数量。有两个表,posts和comments,它们是posts.id和comments上的内部连接 SELECT id, count(post_id) as num_comm from posts INNER JOIN comments on id = post_id WHERE (UNIX_TIMESTAMP(posts.time_posted) - UNIX_TIMESTAMP(comments.time_posted)) &l

我有下面的查询,试图计算出一篇帖子发表后30分钟内的评论数量。有两个表,
posts
comments
,它们是
posts.id
comments上的
内部连接

SELECT id, count(post_id) as num_comm from posts
  INNER JOIN comments on id = post_id
  WHERE (UNIX_TIMESTAMP(posts.time_posted) - UNIX_TIMESTAMP(comments.time_posted)) < (30 * 60)
    AND comments.reply_to_id = 0
  GROUP BY id
  ORDER BY num_comm ASC;
选择id,从posts中将(post_id)计数为num_comm
id=post\u id上的内部连接注释
其中(UNIX_时间戳(posts.time_posted)-UNIX_时间戳(comments.time_posted))<(30*60)
和comments.reply_to_id=0
按id分组
按数量和通信ASC订购;
我遇到的问题是,查询返回的是
num\u comm
结果的总数,而不仅仅是在原始帖子发布后30分钟内发表的、有
评论的评论数。reply\u to\u id
设置为0


如何更改查询以仅返回满足
WHERE
子句中条件的评论数?

推测,帖子是在评论之前发布的。所以,你的价值总是负的。尝试:

SELECT id, count(*) as num_comm 
FROM posts p INNER JOIN
     comments c
     ON p.id = c.post_id
WHERE (UNIX_TIMESTAMP(c.time_posted) - UNIX_TIMESTAMP(p.time_posted)) < (30 * 60) AND
      c.reply_to_id = 0
GROUP BY p.id
ORDER BY num_comm ASC;
选择id,将(*)计数为num\u comm
从立柱p内部连接
评论c
在p.id=c.post\u id上
其中(UNIX_时间戳(c.time_posted)-UNIX_时间戳(p.time_posted))<(30*60)和
c、 答复_至_id=0
按p.id分组
按数量和通信ASC订购;
我真的不喜欢转换为Unix时间戳。我认为代码更清晰,因为:

WHERE c.time_posted < p.time_posted + interval 30 minute AND
      c.reply_to_id = 0
其中c.time_发布
其中的
关闭确实适用于
计数()的计算。你可能误解了结果。或者,
UNIX\u TIMESTAMP()
的计算是错误的(没有样本数据,我们真的不能说)。不管我是否有时间计算,结果num\u comm计数都有相同的计数。啊,废话。今晚喝了几杯。谢谢另外,是否有一个很好的教程来处理带有时间戳的时间计算?我花了很长时间才找到一个,这就是为什么我要使用unix_timestamp()函数。@StevieD。我并不知道有什么好的教程,但我发现自己在MySQL中经常引用日期/时间函数页面。