2013-12-09 57 views
-4

所以我有3個表編寫的SQL語句與JOIN

表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查詢將列出位於巴克斯特大廈球隊的名字,我不能爲我的生活甚至認爲從哪裏開始就這一個,即時通訊相當新的SQL

+1

https://www.google.com/#q=join+statement –

+0

http://dev.mysql.com/doc/refman/5.0/en/join .html –

+1

http://www.google.com/search?q=sql+basics –

回答

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' 
+0

感謝您的幫助,但這給了我一個語法錯誤,說缺少運算符 – Neil

+1

這是sql服務器的語法,如果你使用任何其他的SQL,你應該提到它:)看到它作爲僞代碼,並採用它 – Koryu

+0

抱歉,很多這個工作,一旦我稍微改變它與訪問工作。我不得不把第一次加入括號,這很好! – Neil

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