显示多个表中的值后按 COUNT 对表进行排序

发布于 2025-01-14 06:29:19 字数 2246 浏览 0 评论 0原文

我的目标是:显示特定 ID 在一个表 phpbb_topics 中重复作为 topic_poster 的频率,但前提是也满足正确的 forum_id 条件,然后还显示相应的来自另一个表的用户名,phpbb_users

我已经成功提取了一个特定用户 ID 作为表 phpbb_topics 中的 topic_poster 出现的频率,如下所示:

SELECT topic_poster, COUNT(topic_poster)
FROM phpbb_topics WHERE forum_id = 156
GROUP BY topic_poster

感谢 StackOverflow 上的另一个问题,我现在也知道如何从另一个表获取数据以获取与特定用户 ID 相对应的用户名,就像这样:

SELECT t.topic_poster, u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster

我还设法最终将两者混合以获得我想要的:

SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster WHERE t.forum_id = 156
GROUP BY t.topic_poster

但是,我不知道如何根据计数器正确按降序或升序排序。 phpmyAdmin 不允许我只需单击列的名称即可对其进行排序,并且我使用 GROUP BYORDER BY 编写的任何查询都会报告错误。

更新: 将其放入后:

SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster WHERE t.forum_id = 156
ORDER BY COUNT(topic_poster)

结果仅显示一行:

topic_poster |COUNT(t.topic_poster) | user_id | username
6 | 254 6 | Opix

如果我使用这个,也会发生同样的情况:

SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster WHERE t.forum_id = 156
ORDER BY COUNT(t.topic_poster)

如果我使用这个,也会发生同样的情况:

SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster WHERE t.forum_id = 156
ORDER BY topic_poster

如果我使用这个:SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username FROM phpbb_topics t LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster WHERE t.forum_id = 156 GROUP BY t.topic_poster 我得到了所有结果,但无法按计数器排序。

My goal is: display how often is specific ID repeated as the topic_poster in one table, phpbb_topics, but only if the proper forum_id condition is also met, then also display the corresponding username from another table, phpbb_users.

I have successfully extracted the count of how often is one specific userID occuring as the topic_poster in table phpbb_topics, like that:

SELECT topic_poster, COUNT(topic_poster)
FROM phpbb_topics WHERE forum_id = 156
GROUP BY topic_poster

Thanks to another question on StackOverflow I now also know how to get data from another table to get the username corresponding to the specific userID, like that:

SELECT t.topic_poster, u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster

I also managed to finally mix the two to get what I want:

SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster WHERE t.forum_id = 156
GROUP BY t.topic_poster

However, I do not know how to properly sort in descending or ascending order based on the counter. phpmyAdmin won't let me just click on the column's name to sort by it, and any queries i write with GROUP BY or ORDER BY are reporting errors.

Update:
after putting this in:

SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster WHERE t.forum_id = 156
ORDER BY COUNT(topic_poster)

the results display only one row:

topic_poster |COUNT(t.topic_poster) | user_id | username
6 | 254 6 | Opix

Same happens if I use this:

SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster WHERE t.forum_id = 156
ORDER BY COUNT(t.topic_poster)

Same happens if I use this:

SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster WHERE t.forum_id = 156
ORDER BY topic_poster

If I use this: SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username FROM phpbb_topics t LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster WHERE t.forum_id = 156 GROUP BY t.topic_poster I get all the results, but I can't sort by the counter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2025-01-21 06:29:19

mySQL 扩展了组,所以你不需要不一定要有一个。但是,它假设每列的所有值都相同;因此可以自由地从每列中选择要放入的内容。但是,如果值不同,则它选择的值(1 个值)不能代表整个集合,因此当值不同时必须使用 group by。

换句话说:如果 t.forum_ID = 156 仅限于特定的 topic_poster、user_Id 和用户名。你不会有任何问题。但由于 t.forum_ID 代表每一列中的许多不同值,因此需要分组,否则引擎将“某种程度上”为每个列随机选择一个值。引擎假设所有内容都是相同的。

由此,mySQL 集团的垮台也随之而来。但是,如果所有非聚合列确实具有相同的值......您可以通过允许引擎聚合并为每列“选择”一个值来获得性能增益。

根据您的回答,您认为应该获得多行。这告诉我非聚合字段是不同的,所以添加一个组...

SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster 
WHERE t.forum_id = 156
GROUP BY t.topic_poster, u.user_id, u.username
ORDER BY COUNT(t.topic_poster)

您可能有联系,所以您可能还想在计数后按海报或用户名排序...

mySQL extends the group by so you don't have to have one. However, it assumes all values for each column are the same; so it's free to pick what to put in from each column. However, if the values are different, what it picks (1 value) isn't representative of the entire set, so you must use group by when the values are different.

Put a different way: if t.forum_ID = 156 limited to a specific topic_poster, user_Id and username. you'd have no problem. But since t.forum_ID represents many different values in each of those columns, group by is needed or the engine will "somewhat" randomly select a value for each of them. The engine assumes all are the same.

Thus the downfall of the mySQL Group by extension. But, if all the non-aggregrated columns did have the same value... you get a performance gain by allowing the engine to just aggregate and 'pick' a value for each column.

Based on your response, you think you should be getting multiple rows. So that tells me the non-aggregated fields are different so add a group by...

SELECT t.topic_poster, COUNT(t.topic_poster), u.user_id, u.username
FROM phpbb_topics t
LEFT JOIN phpbb_users u ON u.user_id = t.topic_poster 
WHERE t.forum_id = 156
GROUP BY t.topic_poster, u.user_id, u.username
ORDER BY COUNT(t.topic_poster)

You could have ties, so you may also want to order by poster or user name after the count...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文