SQL:按有多少行排序(使用 COUNT?)

发布于 2024-12-11 07:23:30 字数 551 浏览 1 评论 0原文

对于 SMF,我正在为我的氏族成员制作一个名册(请不要附带“你应该问 SMF”,因为那完全不相关;这只是上下文信息)。

我需要它来选择所有成员(来自 smf_members),并按照他们在 smf_permissions 中拥有的权限数量对其进行排序(以便脚本可以确定谁的级别更高)。 您可以使用以下命令检索有多少权限:COUNT(permission) FROM smf_permissions。

我现在使用此 SQL:

SELECT DISTINCT(m.id_member), m.real_name, m.date_registered  
FROM smf_members AS m, smf_permissions AS p  
WHERE m.id_group=p.id_group  
ORDER BY COUNT(p.permission)

但是,这只返回一行!如何返回行?

干杯, 阿尔特

For SMF, I'm making a roster for the members of my clan (please don't come with "You should ask SMF", because that is completely irrelevant; this is just contextual information).

I need it to select all members (from smf_members) and order it by how many permissions they have in smf_permissions (so the script can determine who is higher in rank).
You can retrieve how many permissions there are by using: COUNT(permission) FROM smf_permissions.

I am now using this SQL:

SELECT DISTINCT(m.id_member), m.real_name, m.date_registered  
FROM smf_members AS m, smf_permissions AS p  
WHERE m.id_group=p.id_group  
ORDER BY COUNT(p.permission)

However, this only returns one row! How to return several rows?

Cheers,
Aart

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

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

发布评论

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

评论(1

平安喜乐 2024-12-18 07:23:30

您需要一个GROUP BY。我还使用显式的 JOIN 语法进行了重写。如果您想要包含零权限的成员,您可能需要更改为 LEFT JOIN

SELECT m.id_member,
       m.real_name,
       m.date_registered,
       COUNT(p.permission) AS N
FROM   smf_members AS m
       JOIN smf_permissions AS p
         ON m.id_group = p.id_group
GROUP  BY m.id_member,
          m.real_name,
          m.date_registered
ORDER  BY COUNT(p.permission)  

You need a GROUP BY. I've also rewritten with explicit JOIN syntax. You might need to change to LEFT JOIN if you want to include members with zero permissions.

SELECT m.id_member,
       m.real_name,
       m.date_registered,
       COUNT(p.permission) AS N
FROM   smf_members AS m
       JOIN smf_permissions AS p
         ON m.id_group = p.id_group
GROUP  BY m.id_member,
          m.real_name,
          m.date_registered
ORDER  BY COUNT(p.permission)  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文