我有2个表 - 主表(称为组)和详细信息(称为用户)
我想返回每个主行以供后续显示,但我只想计算与主节点匹配的详细行数
我的查询无法正常工作 - 它只返回主表中的第一行
$query_string = '
SELECT groups.userGroupID, userGroup,
count(users.userGroupID) AS howMany
FROM groups_table AS groups
JOIN users_table AS users ON users.userGroupID = groups.userGroupID
ORDER BY groups.userGroupID
';
感谢您的帮助。
答案 0 :(得分:9)
通过以下方式忘记你的小组:
SELECT groups.userGroupID, userGroup,
count(users.userGroupID) AS howMany
FROM groups_table AS groups
LEFT JOIN users_table AS users ON users.userGroupID = groups.userGroupID
GROUP BY groups.userGroupID
ORDER BY groups.userGroupID
答案 1 :(得分:-1)
这对我有用,希望它可以帮助其他寻求相同答案的人
SELECT COUNT(howMany) AS fo2
FROM(
SELECT groups.userGroupID, userGroup,
users.userGroupID AS howMany
FROM groups_table AS groups
JOIN users_table AS users ON users.userGroupID = groups.userGroupID
ORDER BY groups.userGroupID) AS myCount