在phpmyadmin中创建视图

时间:2019-08-07 08:23:51

标签: sql phpmyadmin

当我尝试创建像这样的视图时,创建视图data2tables AS

SELECT * FROM Employees e INNER JOIN Computers c ON e.id = c.id WHERE e.name = 'Georgi' AND c.department = 'Sales' 

它给了我一个错误,说#1060-列名“ id”重复,我不知道如何解决它或为什么会发生错误。

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-重复的列名“ id”

2 个答案:

答案 0 :(得分:1)

您的表Employees和表Computers都有一个名为id的列。

当您说SELECT * FROM Employees e INNER JOIN Computers c...时,您基本上是在说给我每一列“雇员”和“计算机” ,因此您得到的是id的副本(以及其他任何列在那里可能是相同的名字。

要使用VIEW,请选择一组定义的结果,因此,我建议您从两个表中明确指定所需的列。

如果两个表都需要id,我建议给联接表(Computers)id列一个别名...类似这样:

CREATE VIEW data2tables AS 
SELECT e.id, e.fieldA, e.fieldN, c.id as ComputersId, c.fieldA, c.fieldN 
FROM Employees e 
INNER JOIN Computers c ON e.id = c.id 
WHERE e.name = 'Georgi' AND c.department = 'Sales';

该别名方法也将应用于您遇到的任何其他跨列名称。

答案 1 :(得分:0)

尝试为计算机表指定列。

CREATE VIEW data2tables AS 
SELECT e.*, c.[column_name].... FROM Employees e INNER JOIN Computers c 
ON e.id = c.id 
WHERE e.name = 'Georgi' AND c.department = 'Sales';