MySQL - View的SELECT包含FROM子句中的子查询

时间:2015-01-31 19:20:50

标签: mysql subquery

我目前正在第一次使用mysql和Views我很好地进入了它但是当我执行这个SQL语句时,我遇到了一个错误。我很遗憾找不到解决方案。它说该视图包含一个子查询,但我无法在此语句中看到或实现任何子查询。

我正在推荐这篇文章:Adding extra column to view, which is not present in table

那个人正在使用类似的代码而且对他而言正在运作,不知道为什么它不在我的例子中。我还查看了Stackoverflow并看到了相同错误的负载,但子查询在其他主题中非常明显。

CREATE OR REPLACE VIEW 
`Worker_!view` AS 
SELECT * FROM 
(
   SELECT `Working_!skill`, 
   'asdf_!electrical' charserver 
   FROM `asdf_!electrical` 
   WHERE 1 ORDER BY `id` DESC LIMIT 1 
   UNION 
   SELECT `Working_!skill`, 'fred_!electrical' charserver 
   FROM `fred_!electrical` WHERE 1 ORDER BY `id` DESC LIMIT 1
);

#1349 - View的SELECT包含FROM子句中的子查询

1 个答案:

答案 0 :(得分:2)

这在MySQL中不起作用。相反,你可以这样做:

CREATE OR REPLACE VIEW `Worker_!view` AS 
   (SELECT `Working_!skill`, 'asdf_!electrical' charserver 
    from `asdf_!electrical` 
    WHERE 1 ORDER BY `id` DESC
    LIMIT 1 
   )
   UNION ALL
   (SELECT `Working_!skill`, 'fred_!electrical' charserver 
    FROM `fred_!electrical`
    WHERE 1
    ORDER BY `id` DESC
    LIMIT 1
  );