【发布时间】: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 答案。它会修复它。