2011-08-02 172 views
7
SELECT people.first_name AS "First Name", people.last_name AS "Last Name", countries.name AS "Country1", territories.name AS "Territory1", cities.name AS "City1", countries.name AS "Country2", territories.name AS "Territory2", cities.name AS "City2" 
FROM adb_people AS people 
JOIN root_cities AS cities ON people.city1 = cities.id 
AND people.city2 = cities.id 
JOIN root_territories AS territories ON people.prov_state1 = territories.id 
AND people.prov_state2 = territories.id 
JOIN root_countries AS countries ON people.country1 = countries.id 

我在這裏要做的是將Country1(id)鏈接到Country1(名稱),並只顯示名稱。 此代碼示例僅適用於如果Country1,Territory1,City1與Country2,Territory2,City2相同MySQL在同一個表上連接多個連接?

我想象我的問題是我如何做我的JOIN。我是新來的東西的SQL方面。我已經在互聯網上閱讀了JOINS(谷歌搜索和閱讀前幾個教程),但是我沒有看到任何幫助。

我真的很感謝任何幫助,我在這裏做錯了。也許在正確的方向推動?

回答

14

對於每個國家/城市/地區,您需要2個單獨的聯合。下面是基本的語法,你可能需要稍微改變它,因爲我還沒有把它通過一個分析器:

SELECT people.first_name AS "First Name", people.last_name AS "Last Name", 
countries1.name AS "Country1", territories1.name AS "Territory1", cities1.name AS "City1", 
countries2.name AS "Country2", territories2.name AS "Territory2", cities2.name AS "City2" 
FROM adb_people AS people 
JOIN root_cities AS cities1 ON people.city1 = cities1.id 
    AND people.city2 = cities1.id 
JOIN root_territories AS territories1 ON people.prov_state1 = territories1.id 
    AND people.prov_state2 = territories1.id 
JOIN root_countries AS countries1 ON people.country1 = countries1.id 
JOIN root_cities AS cities2 ON people.city2 = cities2.id 
    AND people.city2 = cities2.id 
JOIN root_territories AS territories2 ON people.prov_state2 = territories2.id 
    AND people.prov_state2 = territories2.id 
JOIN root_countries AS countries2 ON people.country2 = countries2.id 
+0

呃,我爲什麼不試試這個開頭。我現在將執行它並讓你知道它是如何發生的。謝謝! – rlemon

+0

這個答案還沒有問題,它只適用於city1 = city2嗎? –

+0

它不應該,不。我只是編輯查詢,因爲我忘了在複製/粘貼的選擇部分中更改我的表別名,如果這是你所指的? –

4
SELECT 
    people.first_name AS "First Name", 
    people.last_name AS "Last Name", 
    countries.name AS "Country1", 
    territories.name AS "Territory1", 
    cities.name AS "City1", 
    countries2.name AS "Country2", 
    territories2.name AS "Territory2", 
    cities2.name AS "City2" 
FROM 
    adb_people AS people 
    JOIN root_cities AS cities ON people.city1 = cities.id 
    jOIN root_cities AS cities2 people.city2 = cities2.id 
    JOIN root_territories AS territories ON people.prov_state1 = territories.id 
    JOIN root_territories AS territories2 ON people.prov_state2 = territories2.id 
    JOIN root_countries AS countries ON people.country1 = countries.id 
    JOIN root_countries AS countries2 ON people.country2 = countries2.id 
6

這將這樣的伎倆。

SELECT people.first_name AS "First Name", people.last_name AS "Last Name", countries.name AS "Country1", territories.name AS "Territory1", cities.name AS "City1", countries2.name AS "Country2", territories2.name AS "Territory2", cities2.name AS "City2" 
FROM adb_people AS people 
JOIN root_cities AS cities ON people.city1 = cities.id 
JOIN root_cities AS cities2 ON people.city2 = cities.id 
JOIN root_territories AS territories ON people.prov_state1 = territories.id 
JOIN root_territories AS territories2 ON people.prov_state2 = territories.id 
JOIN root_countries AS countries ON people.country1 = countries.id 
JOIN root_countries AS countries2 ON people.country2 = countries.id