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相同时才有效
我会想象我的问题是我如何做我的加入。我是SQL方面的新手。我已经在互联网上阅读了JOINS(谷歌搜索并阅读了前几个教程),但在这种情况下,我读过的任何内容都没有任何帮助。
我真的很感激我在这里做错了什么。也许正朝着正确的方向努力?
答案 0 :(得分:21)
每个国家/城市/地区需要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
答案 1 :(得分: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
答案 2 :(得分:5)
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