2011-06-27 55 views
3

我有一個數據庫設置來跟蹤錯誤,使用一個配置文件表來跟蹤誰是誰,以及一個包含所有錯誤信息的錯誤表。我感興趣的每個表中的字段有:MYSQL查詢返回多個用戶輸入參數的行數

概況表:

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

我在PHP中製作一個自動化的報表創建器,最終會以Joomla的Plotalot結束,這意味着它必須是一個查詢。自動報告的用戶可以指定他想要在報告中顯示的人的哪些用戶標識。即:

enter IDS: 1,4 

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

該數據庫有超過5,000個錯誤和400個活躍貢獻者。有沒有辦法構建一個查詢來返回格式化的結果,而不使用每個記者的union select?

非常感謝

+0

你說的是COUNT()和左連接?我無法弄清楚UNION與此有何關係: - ? –

+0

UNION SELECT將允許我構建一個基於每個id在for循環在php中的查詢,即 選擇實名,COUNT(*)從配置文件INNER JOIN錯誤ON profiles.userid = bugs.reporter WHERE記者= 1 UNION SELECT .... WHERE記者= 4; – Robert

回答

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 
0
select realname as reporter, count(*) as bugs 
from profiles p join bugs b on p.userid = b.reporter 
group by realname 
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) 
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.