Postgresql查询有两个子查询给出错误:缺少FROM子句?

时间:2013-10-04 15:14:59

标签: sql database postgresql subquery inner-join

我在查询6和8(下面)时遇到了一些问题。

查询6: 我还没想出如何获得人口最多的城市名称。                (其他一切都有效)

我需要做什么:对于每个国家/地区,列出其所有城市的最大人口和该城市的名称。订购 结果按国家/地区按字母顺序排列。

给出错误:查询失败:ERROR:表“city”缺少FROM子句条目LINE 13:ON(city.population = sq.pop)^

查询8: 我还没弄明白如何获取大写名称。                (其他一切都有效)

我需要做什么:对于每个有5-10种语言的国家/地区,请按降序排列所说语言的数量 语言数量以及该国家的首都名称。

给出错误:查询失败:ERROR:表“city”缺少FROM子句条目LINE 13:ON(country.capital = city.id)^

(我不是要求查询的完整解决方案 - 只是修改我的代码的想法)

似乎我需要两个内连接,但是我的语法可能有问题。

单个查询中的两个内部联接是否可以接受?

是否存在语法错误? (我已经检查了在线文档,看起来没问题。但也许我错过了一些东西)。

有没有比两个内部联接更好的方法来尝试这个?

以下是代码的相关片段:(所有其他查询及相关代码正常工作)

    elseif($_POST["query"] == "6")                                          //need help getting name of city with max pop (everything else prints fine)
    {
        $query = "SELECT country.name AS country, largest_city, sq.pop AS population
        FROM lab6.country INNER JOIN 

        (SELECT MAX(city.population) AS pop, country_code 
        FROM lab6.city 
        GROUP BY country_code) AS sq
        USING (country_code)

        INNER JOIN

        (SELECT city.name AS largest_city
        FROM lab6.city) AS sq1
        ON (city.population = sq.pop)

        ORDER BY country.name ASC";
    }
    elseif($_POST["query"] == "8")                      //need help getting capital name (everything else prints fine)
    {
        $query = "SELECT country.name, capital, lang_count
        FROM lab6.country INNER JOIN

        (SELECT count(language) AS lang_count, country_code
        FROM lab6.country_language
        GROUP BY country_code) AS sq
        USING (country_code)

        INNER JOIN

        (SELECT city.name AS capital, city.id
        FROM lab6.city) AS sq1
        ON (country.capital = city.id)

        ORDER BY lang_count DESC, capital DESC";
    }

这已经解决了。这是有用的代码。

    elseif($_POST["query"] == "6")                                          
    {
        $query = "SELECT country.name AS country, largest_city, sq.pop AS population
        FROM lab6.country INNER JOIN 

        (SELECT MAX(city.population) AS pop, country_code 
        FROM lab6.city 
        GROUP BY country_code) AS sq
        USING (country_code)

        INNER JOIN

        (SELECT city.name AS largest_city, city.population
        FROM lab6.city) AS sq1
        ON (sq1.population = sq.pop)

        ORDER BY country.name ASC";
    }
    elseif($_POST["query"] == "8")
    {
        $query = "SELECT country.name, sq1.capital AS capital, lang_count
        FROM lab6.country INNER JOIN

        (SELECT count(language) AS lang_count, country_code
        FROM lab6.country_language
        GROUP BY country_code) AS sq
        USING (country_code)

        INNER JOIN

        (SELECT city.name AS capital, city.id
        FROM lab6.city) AS sq1
        ON (country.capital = sq1.id)

        ORDER BY lang_count DESC, capital DESC";
    }

1 个答案:

答案 0 :(得分:2)

表名“city”被派生表别名“sq1”

覆盖

应该是:

 (SELECT city.name AS capital, city.id
    FROM lab6.city) AS sq1
    ON (country.capital = sq1.id)