Mysql 5.6 / 5.7:View的SELECT包含FROM子句中的子查询

时间:2016-10-20 00:50:17

标签: mysql ubuntu subquery create-view

MySql创建视图:

CREATE VIEW Foo as 
select type from (select 8 as type UNION ALL select 9 as type) types;

Windows上的MySql 5.7.11:有效,但是mysql 5.6.25-0ubuntu0.15.04.1(Ubuntu):无法正常工作。

View's SELECT contains a subquery in the FROM clause

这是从5.6到5.7的变化?我尝试在Ubuntu上将mysql升级到5.7,但在Ubuntu上不可用。有什么建议?感谢。

编辑:该示例是一个测试用例。实际情况是创建一个视图,使Bar的一条记录成为视图中的两条记录:

   CREATE VIEW Foo as 
    select types.type, bar.value from 
    (select 8 as type UNION ALL select 9 as type) types, Bar bar

2 个答案:

答案 0 :(得分:1)

问题在于您使用从另一个查询中选择的查询来创建视图。

所以你应该以不同的方式在你的视图中编写选择查询。


解决方案:

查询:

select type from (select 8 as type UNION ALL select 9 as type) types;

与:

相同
select 8 as type UNION ALL select 9 as type

所以创建一个视图:

CREATE VIEW Foo AS
  select 8 as type UNION ALL select 9 as type;

答案 1 :(得分:1)

是的,这是MySQL的变化。

早期版本的MySQL不允许在存储的视图定义中使用内联视图(或MySQL中的派生表)。

就建议而言......在视图查询中似乎不需要内联视图。除此之外,我在质疑......的必要性,即需要存储视图定义的原因。