简体   繁体   English

带有 GROUP BY 的 SQL JOIN 语句

[英]SQL JOIN statements with GROUP BY

The Problem:问题:

Construct the SQL statement to find the number of attendees for every meeting.构造 SQL 语句以查找每个会议的与会者人数。 Display the following columns:显示以下列:

  • Count of meeting attendees参加会议人数
  • Meeting ID会议编号
  • Meeting start date and time会议开始日期和时间
  • Meeting end date and time会议结束日期和时间

There are 5 tables in this database(person, building, room, meeting, person_meeting这个数据库中有 5 个表(person、building、room、meeting、person_meeting

+-----------+------------+------------+
| person_id | first_name | last_name  |
+-----------+------------+------------+
|         1 | Tom        | Hanks      |
|         2 | Anne       | Hathaway   |
|         3 | Tom        | Cruise     |
|         4 | Meryl      | Streep     |
|         5 | Chris      | Pratt      |
|         6 | Halle      | Berry      |
|         7 | Robert     | De Niro    |
|         8 | Julia      | Roberts    |
|         9 | Denzel     | Washington |
|        10 | Melissa    | McCarthy   |
+-----------+------------+------------+

+-------------+----------------------+
| building_id | building_name        |
+-------------+----------------------+
|           1 | Headquarters         |
|           2 | Main Street Buidling |
+-------------+----------------------+

+---------+-------------+-------------+----------+
| room_id | room_number | building_id | capacity |
+---------+-------------+-------------+----------+
|       1 | 100         |           1 |        5 |
|       2 | 200         |           1 |        4 |
|       3 | 300         |           1 |       10 |
|       4 | 10          |           2 |        4 |
|       5 | 20          |           2 |        4 |
+---------+-------------+-------------+----------+

+------------+---------+---------------------+---------------------+
| meeting_id | room_id | meeting_start       | meeting_end         |
+------------+---------+---------------------+---------------------+
|          1 |       1 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
|          2 |       1 | 2016-12-25 10:00:00 | 2016-12-25 12:00:00 |
|          3 |       1 | 2016-12-25 11:00:00 | 2016-12-25 12:00:00 |
|          4 |       2 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
|          5 |       4 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
|          6 |       5 | 2016-12-25 14:00:00 | 2016-12-25 16:00:00 |
+------------+---------+---------------------+---------------------+

+-----------+------------+
| person_id | meeting_id |
+-----------+------------+
|         1 |          1 |
|        10 |          1 |
|         1 |          2 |
|         2 |          2 |
|         3 |          2 |
|         4 |          2 |
|         5 |          2 |
|         6 |          2 |
|         7 |          2 |
|         8 |          2 |
|         9 |          3 |
|        10 |          3 |
|         1 |          4 |
|         2 |          4 |
|         8 |          5 |
|         9 |          5 |
|         1 |          6 |
|         2 |          6 |
|         3 |          6 |
+-----------+------------+

My SQL statement:我的 SQL 语句:

SELECT Count(person_id) AS “Count of meeting attendees” ,meeting_id,meeting_start,meeting_end    
FROM meeting M ,person_meeting PM    
WHERE M. meeting_id=PM. meeting_id    
Group by PM.meeting_id,M.meeting_start,M.meeting_end;

The error I get:我得到的错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'of meeting attendees” ,meeting_id,meeting_start,meet
ing_end



FROM meeting M ' at line 1

Please Help me, I am not sure What I am doing wrong.请帮助我,我不确定我做错了什么。 I have not been able to find a solution for this problem我一直无法找到解决此问题的方法

Never use commas in the FROM clause.切勿FROM子句中使用逗号。 Always use proper, explicit, standard JOIN syntax.始终使用正确、明确、标准的JOIN语法。

Don't give you columns aliases that need to be escaped.不要给你需要转义的列别名。 So, use underscores rather than spaces.因此,请使用下划线而不是空格。 This fixes your problem with the weird double quote characters:这解决了奇怪的双引号字符的问题:

SELECT COUNT(pm.person_id) AS num_attendees,
       m.meeting_id, m.meeting_start, m.meeting_end    
FROM meeting m JOIN
     person_meeting PM    
     ON m.meeting_id = pm.meeting_id    
GROUP BY m.meeting_id, m.meeting_start, m.meeting_end ;

Based on the error text I would try changing the name of your COUNT() column to just 'Count', since the error message starts just after the first space in your column name根据错误文本,我会尝试将 COUNT() 列的名称更改为“Count”,因为错误消息在列名中的第一个空格之后开始

EDIT:编辑:

I will also second Gordon Linoff's statement: never do this:我还要附上 Gordon Linoff 的声明:永远不要这样做:

FROM tbla a, tblb b
WHERE a.columna = b.columnb

That's a regular old join, and it should be written as such:这是一个常规的旧连接,它应该这样写:

FROM tbla A
JOIN tblb B ON a.columna = b.columnb

There are 2 things that can be changed.有两件事可以改变。 First meeting_id column belongs to both table so you have to define which table's column.第一个 meeting_id 列属于两个表,因此您必须定义哪个表的列。 And then the column naming must have error depending on error description you have provided.然后根据您提供的错误描述,列命名必须有错误。

SELECT Count(person_id) AS "Count" ,M.meeting_id,meeting_start,meeting_end    
FROM meeting M 
INNER JOIN person_meeting PM ON M.meeting_id=PM.meeting_id
Group by M.meeting_id,M.meeting_start,M.meeting_end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM