SQL查询中的列名重复

时间:2011-06-08 06:00:43

标签: mysql sql stored-procedures view mysql-error-1060

在两个具有相同列名的表上创建VIEW时,如何避免MySQL中的重复列名错误

CREATE VIEW db.VI_RegionCity AS SELECT
    Region.Name,
    City.Name
FROM
    db.Region Region,
    db.City City
WHERE
    Region.RegionCode = City.RegionCode



ERROR: Duplicate column name 'Name'

2 个答案:

答案 0 :(得分:4)

使用列名称的别名

CREATE VIEW db.VI_RegionCity AS SELECT
    Region.Name as Region_name,
    City.Name as City_name
...//rest of the query

答案 1 :(得分:1)

CREATE VIEW db.VI_RegionCity AS SELECT
    Region.Name AS RegionName,
    City.Name AS CityName
FROM
    db.Region Region,
    db.City City
WHERE
    Region.RegionCode = City.RegionCode