MySQL JOIN Несколько объединений в одной таблице? - PullRequest
13 голосов
/ 02 августа 2011
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

Я пытаюсь сделать здесь ссылку «Страна1» (id) на «Страна1 (имя)» и отображать только имя.Этот пример кода работает только в том случае, если Country1, Territory1, City1 совпадают с Country2, Territory2, City2

.Я новичок в SQL стороне вещей.Я прочитал о JOINS в Интернете (поиск в Google и прочитал первые несколько уроков), однако ничто из того, что я прочитал, не помогло в этом случае.

Я был бы очень признателен за любую помощь в том, что я делаю здесь неправильно.Может быть, толчок в правильном направлении?

Ответы [ 3 ]

21 голосов
/ 02 августа 2011

вам нужно 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
7 голосов
/ 02 августа 2011

Это сделает свое дело.

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
5 голосов
/ 02 августа 2011
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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...