尝试修复错误后,联盟的MySQL视图无法正常工作

时间:2016-06-06 14:31:05

标签: mysql

我有一个需要联合的mysql 5.6视图。我在这里读到堆栈溢出,在视图中使用union有一个服务器错误。解决方案是删除from中的括号。我这样做但是它不起作用,并希望有人可以帮助我以某种方式得到错误。

我有2个表ordersuccess和orderfailure。视图计算该时间段的成功百分比。时间段为每15米,每小时4小时,15,30,45,00。这是架构,两者都是一样的。计数不会添加到表中。 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子句中有选择,我必须创建多个视图。以下是此查询的创建视图。我删除了所有可能的括号。我错过了什么或者有更好的方法来编写查询吗?

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
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 ;

我按此顺序创建视图。他们成功直到d61P2_14521。

  1. d61P4_14521
  2. d61P5_14521
  3. d61P6_14521
  4. d61P3_14521
  5. d61P2_14521
  6. 失败的视图是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

1 个答案:

答案 0 :(得分:1)

在MySQL 5.7.7之前,您不能在视图的from-clauses(或join-clauses)中使用子查询。因此,您必须在单独的视图中移动这些子查询。您的查询可以(稍微清理后)分成2个视图:

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();