在选择语句中使用视图

时间:2018-10-13 08:57:48

标签: mysql

当尝试使用视图查找有关数据库中表的信息时,似乎无法在select语句中使用视图。

CREATE VIEW Num_projects AS SELECT ENumber, COUNT(*) AS Projects FROM WorksOn Group BY ENumber;

SELECT * FROM Num_projects;

CREATE VIEW Same_num_projects AS SELECT Employee.ENumber, Name FROM Employee JOIN Num_Projects WHERE Projects IN( SELECT Projects FROM Num_projects WHERE ENumber = 00101);

SELECT * FROM Same_num_projects;

这是处理上面的代码的输出:

--------------
CREATE VIEW Same_num_projects AS SELECT Employee.ENumber, Name FROM Employee JOIN Num_Projects WHERE Projects IN( SELECT Projects FROM Num_projects WHERE ENumber = 00101)
--------------

ERROR 1146 (42S02): Table 'csit115.Num_Projects' doesn't exist
--------------
SELECT * FROM Same_num_projects
--------------

ERROR 1146 (42S02): Table 'csit115.Same_num_projects' doesn't exist
--------------

为什么我的观点未被认可?

2 个答案:

答案 0 :(得分:0)

此问题通过将JOIN Num_Projects更改为JOIN Num_projects

来解决。

答案 1 :(得分:0)

这不区分大小写。在创建第二个视图时,您没有定义连接条件,因此缺少data.table。没有打开条件就无法形成内部联接。

CREATE VIEW Num_projects
AS
SELECT
    ENumber
  , COUNT(*) AS Projects
FROM WorksOn
GROUP BY
    ENumber;

SELECT
    *
FROM Num_projects;

CREATE VIEW Same_num_projects
AS
SELECT
    Employee.ENumber
  , Name
FROM Employee
JOIN Num_Projects ON Employee.ENumber = Num_Projects.ENumber
WHERE Projects IN (
    SELECT
        Projects
    FROM Num_projects
    WHERE ENumber = 00101
);

SELECT
    *
FROM Same_num_projects;