SQL:按有多少行排序(使用 COUNT?)
对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一个
GROUP BY
。我还使用显式的JOIN
语法进行了重写。如果您想要包含零权限的成员,您可能需要更改为LEFT JOIN
。You need a
GROUP BY
. I've also rewritten with explicitJOIN
syntax. You might need to change toLEFT JOIN
if you want to include members with zero permissions.