2011-08-02 20 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(name)にリンクして名前だけを表示することです。 このコード例は、Country1、Territory1、City1がCountry2、Territory2、City2と同じ場合にのみ機能します。MySQLは同じテーブルに複数の結合をジョインしますか?

私は自分のJOINをどのようにしているのかイメージします。私は物事のSQL側では新しいです。私はインターネット上のジョインズ(Googleの検索と最初のいくつかのチュートリアルを読む)を読んだことがありますが、私が読んだことは何もこの場合の助けとなっていません。

私がここで間違っていることについては、本当に助けていただきたいと思います。たぶん、正しい方向に微笑んでいますか?

答えて

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 
関連する問題