我有一个数据库设置来跟踪错误,有一个用于跟踪谁是谁的配置文件表,以及一个包含所有错误信息的错误表。我对每个表感兴趣的字段是:
个人资料表:
userid | realname
---------------
1 | Bob
2 | Alice
4 | Carol
...
错误表:
id | reporter | short_desc
-----------------------------
1 | 1 | short description 1
2 | 2 | short description 2
3 | 1 | short description 3
4 | 3 | another short description
其中profiles.userid = bugs.reporter,而bugs.id是特定错误的id
我正在使用PHP创建一个自动化报表创建器,最终将在Jootalla的Plotalot中结束,这意味着它必须是一个查询。自动报告的用户可以指定他希望在报告中显示的人员的用户ID。即:
enter IDS: 1,4
reporter | bugs
--------------
Bob | 2
Carol | 1
该数据库有超过5,000个错误和400个活跃贡献者。有没有办法构建一个查询来返回格式化的结果,而不使用每个记者的联合选择?
非常感谢
答案 0 :(得分:1)
这可能会起到作用:
select u.realname as Reporter, count(b.id) as Bugs
from profiles u INNER JOIN bugs b ON u.userid = b.reporter
where u.userid IN (1,4)
GROUP BY u.userid, u.realname
答案 1 :(得分:0)
select realname as reporter, count(*) as bugs
from profiles p join bugs b on p.userid = b.reporter
group by realname
答案 2 :(得分:0)
SELECT
u.realname as reporter
Count(b.id) as bugs
FROM bugs b
INNER JOIN profiles u on u.userid = b.reporter
WHERE u.userid in (1,4)
答案 3 :(得分:0)
SELECT pr.realname, count(b.id)
FROM profiles pr LEFT JOIN bugs b ON pr.userid = b.reporter
GROUP BY pr.userid, pr.realname
WHERE pr.userid in (1,4 ) -- set your user ids here where report needs to be generated dynamically etc.