【问题标题】:MySQL JOIN Multiple Joins on the same table?MySQL JOIN 同一张表上的多个连接?
【发布时间】:2011-10-18 05:55:21
【问题描述】:
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 相同时才有效

我想我的问题是我如何进行 JOIN。我是 SQL 方面的新手。我已经阅读了互联网上的 JOINS(谷歌搜索并阅读了前几个教程),但是在这种情况下,我所阅读的任何内容都没有任何帮助。

对于我在这里做错的事情,我真的很感激。也许是朝着正确的方向轻推?

【问题讨论】:

    标签: mysql join


    【解决方案1】:

    每个国家/城市/地区需要 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
    

    【讨论】:

    • 呃,我为什么不开始尝试呢。我现在将实施它,并让你知道它是如何进行的。谢谢!
    • 这个答案是不是还有只有city1=city2才有效的问题?
    • 不应该,不。我只是编辑了查询,因为我忘记从复制/粘贴中更改选择部分中的表别名,如果这就是您所指的?
    • 呵呵是的,上面的sql语句是错误的,但是给出的答案是对的。不使用该示例,而是将我的 SQL 语句更改为使用两个标识符进行两次 JOIN 就是答案!
    【解决方案2】:

    这样就可以了。

    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
    

    【讨论】:

      【解决方案3】:
      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-18
        • 2015-07-24
        • 1970-01-01
        • 1970-01-01
        • 2014-05-08
        • 1970-01-01
        相关资源
        最近更新 更多