MYSQL 查询返回多个用户输入参数的行数

发布于 2024-11-17 10:17:43 字数 805 浏览 2 评论 0原文

我有一个数据库设置来跟踪错误,有一个配置文件表来跟踪谁是谁,还有一个错误表包含所有错误的信息。我对每个表感兴趣的字段是:

profiles表:

userid | realname
---------------
1      | Bob
2      | Alice
4      | Carol
...

bugs表:

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是特定bug的ID

我正在用PHP制作一个自动报告创建器,最终将最终出现在 Joomla 的 Plotalot 中,这意味着它必须是一个查询。自动报告的用户可以指定他想要在报告中显示的人员的用户 ID。即:

enter IDS: 1,4

reporter | bugs
--------------
Bob      | 2
Carol    | 1

该数据库有超过 5,000 个错误和 400 个活跃贡献者。有没有什么方法可以构造一个查询来返回类似格式的结果,而无需为每个记者使用 union select?

非常感谢

I have a database setup to track bugs, with a profiles table to keep track of who's who, and a bugs table which contains the information on all of the bugs. The fields i'm interested in each table are:

profiles table:

userid | realname
---------------
1      | Bob
2      | Alice
4      | Carol
...

bugs table:

id | reporter | short_desc
-----------------------------
1  | 1        | short description 1
2  | 2        | short description 2
3  | 1        | short description 3
4  | 3        | another short description

where profiles.userid = bugs.reporter, and bugs.id is the id of the specific bug

I'm making an automated report creator in PHP that will eventually end up in Plotalot for Joomla, which means it has to be one query. The user of the automated report can specify what userid's of the people he wants to show up in the report. ie:

enter IDS: 1,4

reporter | bugs
--------------
Bob      | 2
Carol    | 1

The database has over 5,000 bugs and 400 active contributors. Is there any way to construct a query to return results formatted like that without using union select for each reporter?

Many Thanks

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

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

发布评论

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

评论(4

把昨日还给我 2024-11-24 10:17:43

这可能会起作用:

 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

This might do the trick:

 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
白首有我共你 2024-11-24 10:17:43
select realname as reporter, count(*) as bugs
from profiles p join bugs b on p.userid = b.reporter
group by realname
select realname as reporter, count(*) as bugs
from profiles p join bugs b on p.userid = b.reporter
group by realname
不可一世的女人 2024-11-24 10:17:43
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)
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)
雨落星ぅ辰 2024-11-24 10:17:43
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.
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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文