0

我有两张桌子,我想计算受访者和参与者总数

参与者

  ID  | Name
  -------------
  1     David
  2     John
  3     Mark
  4     Jake

受访者

  ID  | event_id | participant_id
  -------------------------------
  1       1           1
  2       1           2
  3       2           3  
  4       2           1
  5       2           2

结果是这样的

报告

  ID  | event_id | total_respondent |  number_of_participant
  -------------------------------------------------------
  1       1           2                     4
  2       2           3                     4

我的查询是这个..

    SELECT Respondent.event_id, COUNT( Respondent.participant_id) as total_respondent 
FROM  `Respondent`
INNER JOIN participant ON Respondent.participant_id= participant.id
GROUP BY Respondent.event_id

我想在我的查询中包含 number_of_participant .. 查询应该是什么样的。

4

1 回答 1

0

如果您想要两个事件的总参与者数,此查询会有所帮助

SELECT event_id, 
       COUNT(participant_id)  AS total_resondents, 
       (SELECT Count(*) 
        FROM   participants) AS participants
FROM   respondents 
GROUP  BY event_id 

SQL Fiddle 示例http://www.sqlfiddle.com/#!2/1334e/4/0

于 2013-08-29T08:10:13.177 回答