When i try to create view like this CREATE VIEW data2tables AS
SELECT * FROM Employees e INNER JOIN Computers c ON e.id = c.id WHERE e.name = 'Georgi' AND c.department = 'Sales'
it gives me error saying that #1060 – Duplicate column name ‘id’,and i have no idea how to fix it or why does the error occur.
CREATE VIEW data2tables AS
SELECT * FROM Employees e INNER JOIN Computers c
ON e.id = c.id
WHERE e.name = 'Georgi' AND c.department = 'Sales';
#1060 – Duplicate column name ‘id’
2
Answers
try to specify columns for Computer’s table.
Your table
Employees
and the tableComputers
both have a column namedid
.When you say
SELECT * FROM Employees e INNER JOIN Computers c...
you are basically saying give me every column ‘Employees’ AND ‘Computers’ so you are getting a duplicate ofid
(and any other column in there that may be the same name).With a
VIEW
you want to select a defined set of results so I would recommend explicitly specifying the columns you want from both tables.If you require
id
from both tables I would recommend giving the joined table (Computers) id column an alias…something like this:That method of aliasing will also apply to any other cross-over column names you encounter.