mysql - 无法创建包含union的视图

Smu*_*ger 8 mysql union view

我有一个mysql查询,它使用union将多个查询连接到一个结果集中.查询工作得很好.

当我想使用相同的查询来创建视图时,我收到一条错误消息:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union select hulaminloadnumber2,deliveryid,drop1customer from localjhb where hul' at line 2
Run Code Online (Sandbox Code Playgroud)

我的查询完美有效:

select hulaminloadnumber1,deliveryid,drop1customer from localjhb where hulaminloadnumber1>0
union
select hulaminloadnumber2,deliveryid,drop1customer from localjhb where hulaminloadnumber2>0
union
select hulaminloadnumber3,deliveryid,drop1customer from localjhb where hulaminloadnumber3>0
union
select hulaminloadnumber4,deliveryid,drop1customer from localjhb where hulaminloadnumber4>0
union
select hulaminloadnumber5,deliveryid,drop1customer from localjhb where hulaminloadnumber5>0
Run Code Online (Sandbox Code Playgroud)

查询结果

我创建视图的查询是:

create view View_LoadvsCustomer as (
select hulaminloadnumber1,deliveryid,drop1customer from localjhb where hulaminloadnumber1>0
union
select hulaminloadnumber2,deliveryid,drop1customer from localjhb where hulaminloadnumber2>0
union
select hulaminloadnumber3,deliveryid,drop1customer from localjhb where hulaminloadnumber3>0
union
select hulaminloadnumber4,deliveryid,drop1customer from localjhb where hulaminloadnumber4>0
union
select hulaminloadnumber5,deliveryid,drop1customer from localjhb where hulaminloadnumber5>0)
Run Code Online (Sandbox Code Playgroud)

这会在PHPMyadmin中产生以下错误: 错误信息

查询全部来自设计糟糕的表,因此不应存在格式化或排序规则问题.是否可以针对联合查询创建视图?

任何建议表示赞赏.

Ryan度过了一个愉快的周末

Ash*_*n A 30

从视图定义中删除括号.你遇到了这个服务器错误.

  • 谢谢!确切地说是问题和解决方案.为了帮助将来搜索任何人,这是我得到的错误:`ERROR 1064(42000):你的SQL语法有错误; 检查与您的MySQL服务器版本对应的手册,以便在'UNION DISTINCT SELECT ...'附近使用正确的语法 (2认同)