用JOIN编写SQL语句

时间:2013-12-09 17:38:33

标签: sql join

所以我有3张桌子

表1:团队

| team_id   | name   | 
-----------------------
|     1     | alpha  |
|     2     | beta   |
|     3     | gamma  | 

表2:建筑物

| building_id  | name     |
---------------------------
|    1         | Baxter   |
|    2         | LexCorp  |

表3:团队位置

| team_id | building_id |
-------------------------
|    1    |      1      |
|    2    |      1      |
|    3    |      2      |

我现在需要的是一个sql查询,它将列出位于baxter大楼的团队的名字,我不知道我的生活甚至想到从哪里开始,我对SQL很新

2 个答案:

答案 0 :(得分:1)

尝试这个

SELECT team.name FROM team_location
INNER JOIN buildings ON buildings.building_id  = team_location.building_id  
INNER JOIN team ON team.team_id = team_location.team_id 
WHERE buildings.name = 'Baxter'

答案 1 :(得分:0)

SELECT t.NAME
FROM team as t
INNER JOIN teamLocation AS tl ON t.team_id=tl.team_id
INNER JOIN buildings As b ON tl.building_id=b.building_id
WHERE b.name='Baxter'

请检查这个sql小提琴

http://sqlfiddle.com/#!6/45e2e/1