sql查询以查找具有多个与会者的事件的所有会话

时间:2015-05-18 16:52:25

标签: mysql sql

我正在尝试获取一个事件的所有会话,然后计算所有已订阅该事件的与会者(session_attendee表)。

我的架构

5表

活动 - >一天 - >会话

与会者

session_attendee

+------------------------------+
|           attendee           |
+------------------------------+
| id  username    password     |
| 1   user1       test         |
| 2   user2       test         |
| ------                       |
| event                        |
| id  name                     |
| 1   event 1                  |
| 2   event 2                  |
| 3   event 3                  |
| -----                        |
| 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            |
+------------------------------+

我的期望

+--------------+-----------+
| Session Name | Attendees |
+--------------+-----------+
| session a    |         2 |
| session b    |         2 |
| session c    |         0 |
| session d    |         0 |
+--------------+-----------+

我的尝试(可能有一些额外的列)

SELECT day.event_id, session.id, session.name,
(SELECT COUNT(*) FROM day WHERE day.event_id = event.id) AS days,
(SELECT COUNT(distinct attendee_id)) AS attendees
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
WHERE day.event_id = 1
ORDER BY day.date, session.start;

SQL小提琴

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

修改

伙计们,请检查一下,它有效,请提出任何意见。

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

4 个答案:

答案 0 :(得分:0)

从会话s中选择s.name,count(a.id)在s.id = sa.session_id上加入session_attendee sa在a.id = sa.attendee_id group by s.name

上离开加入与会者a

答案 1 :(得分:0)

SELECT s.name
     , COUNT(DISTINCT sa.attendee_id) 
  FROM session s
  LEFT 
  JOIN session_attendee sa
    ON sa.session_id = s.id 
  LEFT 
  JOIN day d
    ON d.id = s.day_id
   AND d.event_id = 1
 GROUP 
    BY s.name

答案 2 :(得分:0)

查询与previous one几乎相同,您只需更改连接顺序即可。现在session表是最左边的表:

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

请参阅fiddle

修改:It's even simpler,无需加入eventday,也无需加入:

SELECT s.name, count(sa.attendee_id)
FROM session s 
LEFT JOIN session_attendee sa
     ON s.id = sa.session_id
GROUP BY s.name;

答案 3 :(得分:0)

非常感谢各位我可以收集到的所有点点滴滴来制作查询,因为您可以看到查询很复杂,我不得不与社区来回反复,但感谢您的耐心等待。如果我在路上遇到类似的打嗝,请耐心等待,只是需求略有不同让我提出另一个问题。

SELECT session.id, session.name, session.start, session.end, session.room,
DATE_FORMAT(day.date, '%m/%d/%Y') AS date, day.name,
(SELECT COUNT(*) FROM day WHERE day.event_id = 1) AS days,
(SELECT COUNT(*) FROM session_attendee WHERE session_attendee.session_id = session.id) AS attendees
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
WHERE event.id = 1
ORDER BY day.date, session.start;