3

How do I avoid a dup column name error in MySQL when creating a VIEW on two tables that both have the same column name as shown here

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'
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
enfield
  • 841
  • 3
  • 16
  • 42

2 Answers2

4

Using alias for column name

CREATE VIEW db.VI_RegionCity AS SELECT
    Region.Name as Region_name,
    City.Name as City_name
...//rest of the query
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • 5
    Is there a way to use "select * ..." and JUST avoiding the duplicates? I have a table with 20 odd fields and only the IDs are in both tables. I don't want to type out all field names. – Rid Iculous Feb 18 '14 at 23:02
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
Stefan H
  • 6,635
  • 4
  • 24
  • 35