【问题标题】:mysql - cannot create view that contains unionmysql - 无法创建包含联合的视图
【发布时间】:2012-04-14 00:51:00
【问题描述】:

我有一个 mysql 查询,它使用联合将多个查询连接到一个结果集中。查询完美运行。

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

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

我的完美查询是:

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

我创建视图的查询是:

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)

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

所有查询都来自设计不佳的表,因此不应存在格式或排序问题。可以针对联合查询创建视图吗?

感谢任何建议。

周末愉快, 瑞恩

【问题讨论】:

  • 所有 SELECT 查询都具有相同的条件 - 'hulaminloadnumber1>0'。对吗?
  • 嗨@Devart,你是对的,抱歉复制了错误的代码。将纠正问题。但是,使用正确的代码会发生相同的错误。谢谢
  • 关于错误 - 看看 Ashwin 的 A 答案。它会修复它。

标签: mysql view union


【解决方案1】:

从视图定义中删除括号。您已访问此服务器bug

【讨论】:

  • 谢谢!正是问题和解决方案。为了帮助将来搜索的任何人,这是我得到的错误:ERROR 1064 (42000): 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 DISTINCT SELECT...
猜你喜欢
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-08
  • 2010-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多