我有一个需要联合的 mysql 5.6 View 。我在堆栈溢出上读到这里存在一个在 View 中使用联合的服务器错误。解决方案是删除 from 中的括号。我这样做了,但它不起作用,希望有人能帮助我以某种方式解决这个错误。
我有 2 个表 ordersuccess 和 orderfailure。该 View 计算该时间段内的成功百分比。时间段为每15m,每小时4个,15、30、45、00。这是架构,两者是相同的。计数 0 不会添加到表中。 null 可以是 0 或 100,具体取决于 ifnull 语句。
+---------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| dtime | datetime | NO | PRI | NULL | |
| counter | bigint(20) | NO | | NULL | |
| rate | bigint(20) | YES | | NULL | |
+---------+------------+------+-----+---------+-------+
我有一个日历表,可以在一段时间内没有任何报告时填补空白。它只有每个时期的日期/时间。这是日历表;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| dtime | datetime | NO | PRI | NULL | |
+-------+----------+------+-----+---------+-------+
这是返回正确数据的查询。
select
'1000591' AS `id`,
`c`.`dtime` AS `dtime`,
'0' AS `rate`,
ifnull(`p`.`percent`,100) AS `counter`
from
(`calendar` `c` left join
(
select
ifnull(f.dtime,s.dtime) as dtime,
ifnull(ifnull(s.counter,0)/(ifnull(s.counter,0) + ifnull(f.counter,0)) * 100,100) as percent
from
(select * from OrderSummarySuccess_14521 where id = 1000591) s
right join
(select * from OrderSummaryFailure_14521 where id = 1000591) f
on s.dtime=f.dtime
union
select
ifnull(f.dtime,s.dtime) as dtime,
ifnull(ifnull(s.counter,0)/(ifnull(s.counter,0) + ifnull(f.counter,0)) * 100,100) as percent
from
(select * from OrderSummarySuccess_14521 where id = 1000591) s
left join
(select * from OrderSummaryFailure_14521 where id = 1000591) f
on s.dtime=f.dtime
order by dtime
) `p`
on((`c`.`dtime` = `p`.`dtime`))
)
where c.dtime < now()`
由于 from 子句中有选择,我必须创建多个 View 。这是我为此查询创建的 View 。我删除了所有能删除的括号。我错过了什么或者有更好的方法来编写查询吗?
CREATE ALGORITHM=UNDEFINED DEFINER=root@localhost SQL SECURITY DEFINER VIEW d61_14521 AS
select '1000591' AS id,
c.dtime AS dtime,
0 AS rate,
ifnull(p.percent,100) AS counter
from calendar c left join d61p2_14521 p on c.dtime = p.dtime;
CREATE ALGORITHM=UNDEFINED DEFINER=root@localhost SQL SECURITY DEFINER
VIEW d61P2_14521 AS select
'1000591' AS id,
c.dtime AS dtime,
'0' AS rate,
ifnull(p.percent,100) AS counter<br/>
from
d61P3_14521
union
d61P6_14521
where c.dtime < now();
CREATE ALGORITHM=UNDEFINED DEFINER=root@localhost SQL SECURITY DEFINER
VIEW d61P3_14521 AS select
ifnull(f.dtime,s.dtime) as dtime,
ifnull(ifnull(s.counter,0)/(ifnull(s.counter,0) + ifnull(f.counter,0)) * 100,100) as percent
from
d61P4_14521 s
right join
d61P5_14521 f
on s.dtime=f.dtime ;
CREATE ALGORITHM=UNDEFINED DEFINER=root@localhost SQL SECURITY DEFINER
VIEW d61P4_14521 AS select * from OrderSummarySuccess_14521 where id = 1000591 ;
CREATE ALGORITHM=UNDEFINED DEFINER=root@localhost SQL SECURITY DEFINER
VIEW d61P5_14521 AS select * from OrderSummaryFailure_14521 where id = 1000591;
CREATE ALGORITHM=UNDEFINED DEFINER=root@localhost SQL SECURITY DEFINER
VIEW d61P6_14521 AS select
ifnull(f.dtime,s.dtime) as dtime,
ifnull(ifnull(s.counter,0)/(ifnull(s.counter,0) + ifnull(f.counter,0)) * 100,100) as percent
from
d61P4_14521 s
left join
d61P5_14521 f
on s.dtime=f.dtime ;
我按此顺序创建 View 。他们成功直到 d61P2_14521。
- d61P4_14521
- d61P5_14521
- d61P6_14521
- d61P3_14521
- d61P2_14521
失败的 View 是 d61P2_14521。这是错误。
mysql> CREATE ALGORITHM=UNDEFINED DEFINER=root@localhost SQL SECURITY DEFINER
-> VIEW d61P2_14521 AS select
-> '1000591' AS id,
-> c.dtime AS dtime,
-> '0' AS rate,
-> ifnull(p.percent,100) AS counter
-> from
-> d61P3_14521
-> union
-> d61P6_14521
-> where c.dtime < now();
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 'vod61P6_14521
where c.dtime < now()' at line 10
最佳答案
在 MySQL 5.7.7 之前,不能在 View 的 from 子句(或 join 子句)中使用子查询。因此,您必须在单独的 View 中移动这些子查询。您的查询可以(经过一些清理后)分为 2 个 View :
create viewSubqueryFullOuterJoin as
select
ifnull(f.dtime,s.dtime) as dtime,
ifnull(ifnull(s.counter,0)/(ifnull(s.counter,0)
+ ifnull(f.counter,0)) * 100,100) as percent
from OrderSummarySuccess_14521 s
right join OrderSummaryFailure_14521 f
on s.id = 1000591 and f.id = 1000591 and s.dtime=f.dtime
union
select
ifnull(f.dtime,s.dtime) as dtime,
ifnull(ifnull(s.counter,0)/(ifnull(s.counter,0)
+ ifnull(f.counter,0)) * 100,100) as percent
from OrderSummarySuccess_14521 s
left join OrderSummaryFailure_14521 f
on s.id = 1000591 and f.id = 1000591 and s.dtime=f.dtime
order by dtime; -- 'order by' might belong in viewQueryWithSubquery
create viewQueryWithSubquery as
select
'1000591' AS `id`,
`c`.`dtime` AS `dtime`,
'0' AS `rate`,
ifnull(`p`.`percent`,100) AS `counter`
from `calendar` `c`
left join viewSubqueryFullOuterJoin `p`
on `c`.`dtime` = `p`.`dtime`
where `c`.dtime < now();
关于尝试修复错误后,带有联合的 MySQL View 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37660101/