2011-08-02 8 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 검색 및 처음 몇 자습서를 읽음)에서 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