2014-06-11 119 views
0

我試圖聯合多個表,只得到一些字段的示數加入多個表

這裏是我的SQL代碼:

SELECT t1.match_id, t1.provider_id, t1.visible, t2.match_type, t3.class_num, t3.location_id, t4.prov_name, t6.city_name 
    FROM match AS t1 
     JOIN umtc_class AS t2 
      on t1.match_id = t2.match_id 
     JOIN abroad_class AS t3 
      on t1.match_id = t3.match_id 
     JOIN provider AS t4 
      on t1.provider_id = t3.provider_id 
     JOIN location AS t5 
      on t3.location_id = t3.location_id 
     JOIN location AS t6 
      on t5.city_id = t6.city_id 

1064 - 你在你的SQL有一個錯誤句法;檢查對應於你的MySQL服務器版本在第2行

我一直用它瞎搞使用近「比賽爲T1

JOIN umtc_class AS t2 
     on t1.match_id = t2.match_id 

」正確的語法手冊,但我不能讓任何地方。 ...有什麼想法,爲什麼它不工作?

+0

可能不是你的語法錯誤,但你可能並不想這樣做(下面T5加入):上t3.location_id = t3.location_id – BryPie

+0

另外,你的意思是加入位置表兩次? – BryPie

+0

它往往更容易發現問題,這樣,如果你的表的別名有有意義的名字,而不是T1,T2,T3等 – Siyual

回答

1

MATCH是函數名,因而保留的字。嘗試使用完全限定的表名和數據庫名稱前綴:

SELECT ... 
FROM mydb.match AS t1 
+0

從M.Ali幫助後能得到正確的選擇我的表(它一直的那些日子裏,一個),這是真正發生的問題。謝謝! – tathyler

3
FROM match AS t1 
    JOIN umtc_class AS t2 
     on t1.match_id = t2.match_id 
    JOIN abroad_class AS t3 
     on t1.match_id = t3.match_id 
    JOIN provider AS t4 
     on t1.provider_id = t3.provider_id --<-- This ON clause should have t4 in it 
    JOIN location AS t5 
     on t3.location_id = t3.location_id --<-- This ON clause should have t5 in it 
    JOIN location AS t6 
     on t5.city_id = t6.city_id 

ON任何條款應定義表名之間的關係遵循和前述結果從表中設定或表的連接。

0
SELECT t1.match_id, t1.provider_id, t1.visible, t2.match_type, t3.class_num, t3.location_id, t4.prov_name, t6.city_name 
    FROM match AS t1 
     JOIN umtc_class AS t2 
      on t1.match_id = t2.match_id 
     JOIN abroad_class AS t3 
      on t1.match_id = t3.match_id 
     JOIN provider AS t4 
      on t1.provider_id = t4.provider_id //here was mistake 
     JOIN location AS t5 
      on t3.location_id = t5.location_id //here was mistake 
     JOIN location AS t6 
      on t5.city_id = t6.city_id 
0

而是採用匹配嘗試使用[搭配]即封閉之間的匹配[和]