遇到外键查询问题

问题描述

| 我是sql的新手,我很难弄清楚如何在MysqL Workbench上使用外键执行查询。 在我的示例中,我有三个表:
people
places
people_places
。 在
people
中,主键是
people_id
,并且存在一个名为
name
的列,其中包含某人的名字。 在
places
中,主键是
places_id
,并且存在一个名为
placename
的列,并带有一个地点名称
People_places
是具有三列的联结表:
idpeople_places
(主键),
people_id
(外键)和
places_id
(外键)。因此,此表使用其他两个表中的数字ID将人与地点相关联。 假设我想要与地点#3相关联的所有人的名字。因此,people_places表具有按数字的关联,而people表则将这些数字与我想要的实际名称相关联。 我将如何执行该查询?     

解决方法

        尝试使用此方法查找与地点ID 3相关联的所有人员名称。
SELECT p.name
FROM people as p
INNER JOIN people_places as pp on pp.people_id = p.people_id
WHERE pp.places_id = 3
    ,        好的,因此您需要将所有三个表“拼接”在一起,是吗? 像这样:
select people.name
from   people        -- 1. I like to start with the table(s) that I want data from,and,people_places -- 2. then the \"joining\" table(s),places        -- 3. finally the table(s) used \"just\" for filtering.
where  people.people_id = people_places.people_id  -- join table 1 to table 2
and    people_places.place_id = places.place_id    -- join table 2 to table 3
and    places.name = \"BERMUDA\"                     -- restrict rows in table 3
我确定您可以做剩下的事。 干杯。基思