Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.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 按帖子数量排列线程_Mysql - Fatal编程技术网

Mysql 按帖子数量排列线程

Mysql 按帖子数量排列线程,mysql,Mysql,我有一张有线的桌子和一张有柱子的桌子。我想按与它们相关联的帖子的数量列出它们 我的表的示例结构 帖子表: id creator replyTo text timestamp 1 1 1 Bla 2011-11-11 11:11 2 2 2 Alb 2011-11-11 11:11 3 3 3 Lba 2011-11-11 11

我有一张有线的桌子和一张有柱子的桌子。我想按与它们相关联的帖子的数量列出它们

我的表的示例结构

帖子表:

id   creator    replyTo    text    timestamp
1    1          1          Bla     2011-11-11 11:11
2    2          2          Alb     2011-11-11 11:11
3    3          3          Lba     2011-11-11 11:11
4    4          1          Lab     2011-11-11 11:11
5    5          2          Bal     2011-11-11 11:11
6    2          2          Abl     2011-11-11 11:11
id  creator     name       content    timestamp
1   1           BlaBla     BlaBla     2011-11-11 11:11
2   3           AlbAlb     AlbAlb     2011-11-11 11:11
3   2           LbaLba     LbaLab     2011-11-11 11:11
id  creator     name       count      timestamp
2   3           AlbAlb     3          2011-11-11 11:11
1   1           BlaBla     2          2011-11-11 11:11
3   2           LbaLba     1          2011-11-11 11:11
线程表:

id   creator    replyTo    text    timestamp
1    1          1          Bla     2011-11-11 11:11
2    2          2          Alb     2011-11-11 11:11
3    3          3          Lba     2011-11-11 11:11
4    4          1          Lab     2011-11-11 11:11
5    5          2          Bal     2011-11-11 11:11
6    2          2          Abl     2011-11-11 11:11
id  creator     name       content    timestamp
1   1           BlaBla     BlaBla     2011-11-11 11:11
2   3           AlbAlb     AlbAlb     2011-11-11 11:11
3   2           LbaLba     LbaLab     2011-11-11 11:11
id  creator     name       count      timestamp
2   3           AlbAlb     3          2011-11-11 11:11
1   1           BlaBla     2          2011-11-11 11:11
3   2           LbaLba     1          2011-11-11 11:11
示例输出:

id   creator    replyTo    text    timestamp
1    1          1          Bla     2011-11-11 11:11
2    2          2          Alb     2011-11-11 11:11
3    3          3          Lba     2011-11-11 11:11
4    4          1          Lab     2011-11-11 11:11
5    5          2          Bal     2011-11-11 11:11
6    2          2          Abl     2011-11-11 11:11
id  creator     name       content    timestamp
1   1           BlaBla     BlaBla     2011-11-11 11:11
2   3           AlbAlb     AlbAlb     2011-11-11 11:11
3   2           LbaLba     LbaLab     2011-11-11 11:11
id  creator     name       count      timestamp
2   3           AlbAlb     3          2011-11-11 11:11
1   1           BlaBla     2          2011-11-11 11:11
3   2           LbaLba     1          2011-11-11 11:11
方法二:

SELECT *, (SELECT COUNT(*) FROM Posts WHERE Threads.id = Posts.replyTo) post_count
FROM Threads
ORDER BY post_count
注意:您的列名不好。您不应该将所有ID命名为相同的名称,这会使连接表变得非常困难。将
replyTo
更改为
thread\u id
,将线程的
id
更改为
thread\u id
,并将posts更改为
post\u id
尝试以下操作:

SELECT t.id, t.creator, t.name, count(*) AS ct, t.timestamp
FROM   threads t
JOIN   posts p ON p.replyTo = t.id
GROUP  BY 1
ORDER  BY count(*) DESC, 1,2;
精确生成所需的结果。

是的缩写(因为在mysql中按主键分组就足够了):

这是的缩写(因为这些是位置参数):

对评论中其他问题的回答 添加一个
WHERE
子句,如下所示:

SELECT t.id, t.creator, t.name, count(*) AS ct, t.timestamp
FROM   threads t
JOIN   posts p ON p.replyTo = t.id
WHERE  t.timestamp BETWEEN (CURRENT_TIMESTAMP - INTERVAL 24 HOUR)
                       AND CURRENT_TIMESTAMP 
GROUP  BY 1
ORDER  BY count(*) DESC, 1,2;

您的两个选项都缺少OP输出中的
count
列。POST和线程是如何连接的?Does
posts.replyto
reference
threads.id
?@ErwinBrandstetter,这是正确的。额外的问题,如果我想将我的请求限制为仅在过去24小时内创建的线程,我应该怎么做?@Victor:我在我的答案中添加了一个答案。@Erwin,太棒了!谢谢你的回答!我认为它可以工作,但在大型数据库上它会变得很慢。@MosheL:这句话很简单。如果您不顾一切,可以使用适当的索引或物化视图来加快速度。可能是按天分组,而不是动态24小时窗口(对于第二个答案)。