使用世界表上的UNION查询重复列名称错误

时间:2017-12-05 04:39:58

标签: mysql sql

我正在尝试通过国家/地区代码对World数据库的三个表中构建的Mysql进行完全连接。我想计算返回的行数。

这是我写的:

    SELECT * From city 
 LEFT JOIN country 
        ON city.CountryCode = country.Code
 LEFT JOIN countrylanguage 
        ON country.Code = countrylanguage.CountryCode
     UNION
    SELECT * from city 
RIGHT JOIN country 
        ON city.CountryCode = country.Code
RIGHT JOIN countrylanguage 
        ON country.Code = countrylanguage.CountryCode;

这返回了30671行。 如果我将COUNT(*)子句包装在外面,

    SELECT COUNT(*) FROM (
    SELECT * from city 
 LEFT JOIN country 
        ON city.CountryCode = country.Code
 LEFT JOIN countrylanguage 
        ON country.Code = countrylanguage.CountryCode
     UNION
    SELECT * from city 
RIGHT JOIN country 
        ON city.CountryCode = country.Code
RIGHT JOIN countrylanguage 
        ON country.Code = countrylanguage.CountryCode) AS B;
它升起了 Error Code: 1060. Duplicate column name 'Name'

我试着用另一种方式做到这一点:

    SELECT COUNT(*) from (
    SELECT c1.* from city AS c1
 LEFT JOIN country AS co1
        ON c1.CountryCode = co1.Code
 LEFT JOIN countrylanguage AS cl1
        ON co1.Code = cl1.CountryCode 
     UNION 
    SELECT c2.* from city AS c2
RIGHT JOIN country AS co2
        ON c2.CountryCode = co2.Code
RIGHT JOIN countrylanguage AS cl2
        ON co2.Code = cl2.CountryCode) AS B;

只有4080行。

我不确定哪一个是正确的方法?

谢谢

2 个答案:

答案 0 :(得分:1)

一般来说,子查询不能包含两个具有相同名称的列,否则会出现错误1060。

解决方法是将*替换为完整的列列表,必要时使用别名:

SELECT COUNT(*) FROM (
        SELECT
               city.ID, city.Name, city.CountryCode, city.District, city.Population,
               country.Code, country.Name AS country_Name, country.Continent, country.Region, country.SurfaceArea, country.IndepYear, country.Population AS country_Population, country.LifeExpectancy, country.GNP, country.GNPOld, country.LocalName, country.GovernmentForm, country.HeadOfState, country.Capital, country.Code2,
               countrylanguage.CountryCode AS countrylanguage_CountryCode, countrylanguage.Language, countrylanguage.IsOfficial, countrylanguage.Percentage
          FROM city
     LEFT JOIN country

但是,您对所有列都不感兴趣,只是足以获得不同组合的数量。因此,您的查询简化为:

SELECT COUNT(*) FROM (
        SELECT city.ID, country.Code, countrylanguage.CountryCode, countrylanguage.Language From city
     LEFT JOIN country
            ON city.CountryCode = country.Code
     LEFT JOIN countrylanguage
            ON country.Code = countrylanguage.CountryCode
         UNION
        SELECT city.ID, country.Code, countrylanguage.CountryCode, countrylanguage.Language From city
    RIGHT JOIN country
            ON city.CountryCode = country.Code
    RIGHT JOIN countrylanguage
            ON country.Code = countrylanguage.CountryCode
) AS B;

-- 30671

答案 1 :(得分:0)

你得到错误

  

重复列名称'名称'

因为您的联接中有多列 名称

在正常查询中,这不是问题,但count(*)很重要。您必须为 name 列提供别名,例如

city.name AS city_name
country.name AS country_name
countrylanguage.name AS countrylanguage_name

另见Duplicate column name on JOIN in mysql

或者,如果您只需要计数,则可以从select中删除不需要的列,例如更改

SELECT *

SELECT id -- or any appropriate column

然后获取count(id)count(*)