sql连接语句

fzwojiic  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(378)

问题是:
构造sql语句以查找每个会议的与会者数量。显示以下列:
会议出席人数
会议id
会议开始日期和时间
会议结束日期和时间
此数据库中有5个表(person、building、room、meeting、person\u 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 |
+-----------+------------+

我的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;

我得到的错误是:

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

请帮帮我,我不知道我做错了什么。我没能找到解决这个问题的办法

5t7ly7z5

5t7ly7z51#

基于错误文本,我将尝试将count()列的名称改为'count',因为错误消息在列名的第一个空格之后开始
编辑:
我也会支持gordon linoff的说法:永远不要这样做:

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

这是一个常规的旧连接,应该这样写:

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

cuxqih212#

不要在句子中使用逗号 FROM 条款。始终使用适当、明确、标准 JOIN 语法。
不要给你需要转义的列别名。所以,使用下划线而不是空格。这解决了你的问题,奇怪的双引号字符:

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 ;
v8wbuo2f

v8wbuo2f3#

有两件事是可以改变的。第一个表的id列属于这两个表,因此必须定义哪个表的列。然后根据您提供的错误描述,列命名必须有错误。

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;

相关问题