sql查找事件的参与者数(已加入)

时间:2015-05-18 01:27:37

标签: mysql sql

我试图找到实际参加活动会议的与会者人数。我有5张桌子。

event -> day -> session

attendee

session_attendee

模式

+------------------------------+
|           attendee           |
+------------------------------+
| id  username    password     |
| 1   user1       test         |
| 2   user2       test         |
| ------                       |
| event                        |
| id  name                     |
| 1   event 1                  |
| 2   event 2                  |
| -----                        |
| day                          |
| id  date            event_id |
| 1   '2015-06-01'    1        |
| 2   '2015-06-02'    1        |
| 3   '2015-07-01'    2        |
| 4   '2015-07-02'    2        |
| ------                       |
| session                      |
| id  name        day_id       |
| 1   session a   1            |
| 2   session b   1            |
| 3   session c   2            |
| 4   session d   2            |
| ------                       |
| session_attendee             |
| id  session_id  attendee_id  |
| 1   1           1            |
| 2   2           1            |
| 3   1           2            |
| 4   2           2            |
+------------------------------+

期望

+-----------------------------+
|  id  name        attendees  |
+-----------------------------+
| 1   'event 1'   2           |
| 2   'event 2'   0/null etc. |
+-----------------------------+

子查询

SQL查询我尝试过使用连接但是它返回了多条记录(即我不能将它用作子查询)

SELECT count(*) FROM event
INNER JOIN day ON event.id = day.event_id 
INNER JOIN session ON day.id = session.day_id
INNER JOIN session_attendee ON session.id = session_attendee.session_id
WHERE event.id = 1
GROUP BY attendee_id

家长查询

以上子查询替换

SELECT id, name,  
(SELECT COUNT(*)  FROM day WHERE day.event_id = event.id) AS days,
(SELECT COUNT(*) FROM event
INNER JOIN day ON event.id = day.event_id 
INNER JOIN session ON day.id = session.day_id
INNER JOIN session_attendee ON session.id = session_attendee.session_id
GROUP BY session_attendee.attendee_id) AS attendees
FROM event;

SQL小提琴

http://sqlfiddle.com/#!9/72ffc

更多工作:

SELECT event.id, event.name,
(SELECT count(*) FROM event e INNER JOIN day AS d ON e.id = d.event_id WHERE e.id = event.id) AS total_days,
day.date, session.name, session_attendee.attendee_id
FROM event
LEFT JOIN day ON event.id = day.event_id
LEFT JOIN session ON day.id = session.day_id
LEFT JOIN session_attendee ON session.id = session_attendee.session_id
ORDER BY event.id;

- COUNT(distinct attendee_id)AS与会者

只要我输入COUNT(distinct attendee_id),我的结果集就会减少到一行,即我无法查看其他事件的记录。

2 个答案:

答案 0 :(得分:0)

您的第一个查询看起来基本上只有一个例外:如果您想要事件的信息,您应该按事件ID聚合,而不是参与者ID:

SELECT e.id, e.name, count(*)
FROM event e INNER JOIN
     day d
     ON e.id = d.event_id INNER JOIN
     session s
     ON d.id = s.day_id INNER JOIN
     session_attendee sa
     ON s.id = sa.session_id
GROUP BY e.id;

当然,如果您只想要一个事件的信息,可以将GROUP BY替换为:

WHERE e.id = 1

编辑:

您希望LEFT JOIN保留所有事件:

SELECT e.id, e.name, count(distinct sa.attendee_id)
FROM event e LEFT JOIN
     day d
     ON e.id = d.event_id LEFT JOIN
     session s
     ON d.id = s.day_id LEFT JOIN
     session_attendee sa
     ON s.id = sa.session_id
GROUP BY e.id;

答案 1 :(得分:0)

试试这个

SELECT event.id, name,
   (SELECT COUNT(*)  FROM day WHERE day.event_id = event.id) AS days,
   b.number_of_attendee
   FROM event
   left join (
           SELECT a.id, count(*) as number_of_attendee from
           (
            SELECT event.id, attendee_id FROM event
            INNER JOIN day ON event.id = day.event_id 
            INNER JOIN session ON day.id = session.day_id
            INNER JOIN session_attendee ON session.id = session_attendee.session_id
            group by event.id, attendee_id
           ) as a
           group by a.id) as b

   on b.id = event.id

的结果如

id  name    days    number_of_attendee
1   Event 1     2   2
2   Event 2     2   (null)