无法从其他视图创建mysql视图

时间:2014-03-28 11:57:32

标签: mysql view mysql-error-1064 mariadb

我有以下观点

M_VWPROC_sub1,M_VWPROC_sub2,M_VWPROC_sub3,M_VWPROC_sub4,M_VWPROC_sub5, M_VWPROC_sub6,M_VWPROC_sub7,M_VWPROC_sub8,M_VWPROC_sub9

我尝试使用以下sql创建视图。但是我收到以下错误

  

#1064 - 您的SQL语法出错;查看与MariaDB服务器版本对应的手册,以获取正确的语法   'M_VWPROC_sub2 union附近所有M_VWPROC_sub3联合所有M_VWPROC_sub4   联合所有M_VWPROC'在第2行

查询:

CREATE OR REPLACE  VIEW M_VWPROC (UNIT, PS_DATE, LOCALIN, STOCKIN, STOCKOUT) AS 
select inner_sub_table.UNIT, 
       inner_sub_table.PS_DATE, 
       sum(inner_sub_table.LOCALIN)LOCALIN, 
       sum(inner_sub_table.pur) STOCKIN, 
       sum(inner_sub_table.sale) STOCKOUT 
  from M_VWPROC_sub1 
 union all M_VWPROC_sub2 
 union all M_VWPROC_sub3 
 union all M_VWPROC_sub4 
 union all M_VWPROC_sub5
 union all M_VWPROC_sub6 
 union all M_VWPROC_sub7 
 union all M_VWPROC_sub8 
 union all M_VWPROC_sub9) 
       inner_sub_table
 group by inner_sub_table.UNIT, inner_sub_table.PS_DATE;

任何帮助。提前致谢。

1 个答案:

答案 0 :(得分:0)

union all的正确语法需要子查询。 。 。这是一个select子句。但是,你不能做你想做的事。您需要另一层子查询。类似的东西:

create view v_YetAnotherView as 
 select * from M_VWPROC_sub1 
 union all select * from M_VWPROC_sub2 
 union all select * from M_VWPROC_sub3 
 union all select * from M_VWPROC_sub4 
 union all select * from M_VWPROC_sub5
 union all select * from M_VWPROC_sub6 
 union all select * from M_VWPROC_sub7 
 union all select * from M_VWPROC_sub8 
 union all select * from M_VWPROC_sub9;

然后:

CREATE OR REPLACE  VIEW M_VWPROC (UNIT, PS_DATE, LOCALIN, STOCKIN, STOCKOUT) AS 
select inner_sub_table.UNIT, 
       inner_sub_table.PS_DATE, 
       sum(yav.LOCALIN)LOCALIN, 
       sum(yav.pur) STOCKIN, 
       sum(yav.sale) STOCKOUT 
  from YetAnotherView yav
  group by yav.UNIT, yav.PS_DATE;

请注意,对视图的这种荒谬的限制(from子句不能包含子查询)仅限于MySQL。