2

我需要比较同一列中的行,所以我有以下 mysql 查询,它可以很好地给出预期的结果。

SELECT x.aord, 
       x.anode AS parent, 
       x.bnode AS child 
FROM   (SELECT a.ordinal               AS aord, 
               a.id_dt_graph_node_edge AS aid, 
               a.id_dt_graph_node      AS anode, 
               b.ordinal               AS bord, 
               b.id_dt_graph_node_edge AS bid, 
               b.id_dt_graph_node      AS bnode 
        FROM   dt_graph_node_edge a 
               JOIN dt_graph_node_edge b 
                 ON a.ordinal < b.ordinal) x 
       LEFT JOIN (SELECT a.ordinal               AS aord, 
                         a.id_dt_graph_node_edge AS aid, 
                         a.id_dt_graph_node      AS anode, 
                         b.ordinal               AS bord, 
                         b.id_dt_graph_node_edge AS bid, 
                         b.id_dt_graph_node      AS bnode 
                  FROM   dt_graph_node_edge a 
                         JOIN dt_graph_node_edge b 
                           ON a.ordinal < b.ordinal) y 
              ON x.aord = y.aord 
                 AND x.bord > y.bord 
WHERE  y.bord IS NULL 
ORDER  BY x.aord, 
          x.bord 

我发现由于错误 #1349,无法在此查询上创建视图。任何人都可以提出一种更好的方法来进行这样的查询,特别关注速度,实际上这个查询非常慢。谢谢。

4

1 回答 1

0

您无法VIEW从查询创建 a 的原因是它包含一个子查询。VIEW,根据文档,不能在查询中包含子SELECT查询。解决方法是在子查询上创建VIEW第一个,例如

CREATE VIEW firstSubquery
AS
SELECT a.ordinal               AS aord, 
       a.id_dt_graph_node_edge AS aid, 
       a.id_dt_graph_node      AS anode, 
       b.ordinal               AS bord, 
       b.id_dt_graph_node_edge AS bid, 
       b.id_dt_graph_node      AS bnode 
FROM   dt_graph_node_edge a 
       JOIN dt_graph_node_edge b 
         ON a.ordinal < b.ordinal;

CREATE VIEW secondSubquery
AS
SELECT  a.ordinal               AS aord, 
        a.id_dt_graph_node_edge AS aid, 
        a.id_dt_graph_node      AS anode, 
        b.ordinal               AS bord, 
        b.id_dt_graph_node_edge AS bid, 
        b.id_dt_graph_node      AS bnode 
FROM   dt_graph_node_edge a 
        JOIN dt_graph_node_edge b 
        ON a.ordinal < b.ordinal;

VIEW并加入当前查询中新创建的,

CREATE VIEW finalVIEW
AS
SELECT x.aord, 
       x.anode AS parent, 
       x.bnode AS child 
FROM   firstSubquery x 
       LEFT JOIN secondSubquery y 
              ON x.aord = y.aord AND x.bord > y.bord 
WHERE  y.bord IS NULL 
ORDER  BY x.aord,  x.bord 

PS:MySQL 中的 VIEWS 太可怕了

于 2013-04-09T02:59:03.240 回答