mysql-common表表达式:模拟完全联接

bweufnob  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(310)

我有多个(超过20)cte的,我需要连接在一起,输出并排行。

WITH 
cte_lnz AS (SELECT n.name, ROW_NUMBER() OVER (ORDER BY e.id) AS rn_lnz FROM entries e LEFT JOIN nodes n ON n.id = e.node_id LEFT JOIN attribs a ON a.id = e.attrib_id 
WHERE a.name = 'LOCATION' AND e.value = 'Gruberstrasse' AND DATE(ts) = CURRENT_DATE ORDER BY e.id         
), 
cte_wbg AS (SELECT n.name, ROW_NUMBER() OVER (ORDER BY e.id) AS rn_wbg FROM entries e LEFT JOIN nodes n ON n.id = e.node_id LEFT JOIN attribs a ON a.id = e.attrib_id
WHERE  a.name = 'LOCATION' AND e.value = 'Wienerberg' AND DATE(ts) = CURRENT_DATE ORDER BY e.id         
), 
cte_gbg AS ( SELECT n.name, ROW_NUMBER() OVER (ORDER BY e.id) AS rn_gbg FROM entries e LEFT JOIN nodes n ON n.id = e.node_id LEFT JOIN attribs a ON a.id = e.attrib_id
WHERE  a.name = 'LOCATION' AND e.value = 'Geiselberg' AND DATE(ts) = CURRENT_DATE ORDER BY e.id        
),
cte_vie AS ( SELECT n.name, ROW_NUMBER() OVER (ORDER BY e.id) AS rn_vie FROM entries e LEFT JOIN nodes n ON n.id = e.node_id LEFT JOIN attribs a ON a.id = e.attrib_id
WHERE  a.name = 'LOCATION' AND e.value IN ('Geiselberg', 'Wienerberg') AND DATE(ts) = CURRENT_DATE ORDER BY e.id         
)

这适用于前两个cte。。。

-- simulated full join
    SELECT lnz.name LNZ, wbg.name WBG
    FROM cte_lnz lnz LEFT JOIN cte_wbg wbg
    ON wbg.rn_wbg = lnz.rn_lnz
    UNION
    SELECT lnz.name LNZ, wbg.name WBG
    FROM cte_wbg wbg LEFT JOIN cte_lnz lnz
    ON wbg.rn_wbg = lnz.rn_lnz

+----------------------+------------------------------+
| LNZ                  | WBG                          |
+----------------------+------------------------------+
| STP17T1_SGKKT1       | AIXHVBDBT                    |
| AIXSAGRU3            | KUG0167_JBOSS-T4             |
| ARR5S1P9_TIC         | KUG0169_RMAN_HA              |
| AIXBUILDHOST         | KUG0121_ZPVTT1               |

这是一个错误。。。

SELECT lnz.name LNZ, wbg.name WBG
    FROM cte_lnz lnz LEFT JOIN cte_wbg wbg
    ON wbg.rn_wbg = lnz.rn_lnz
    UNION
    SELECT lnz.name LNZ, wbg.name WBG
    FROM cte_wbg wbg LEFT JOIN cte_lnz lnz
    ON wbg.rn_wbg = lnz.rn_lnz
-- just a placeholder
    SELECT gbg.name GBG, vie.name VIE
    FROM cte_gbg gbg LEFT JOIN cte_vie vie
    ON vie.rn_vie = gbg.rn_gbg
    UNION
    SELECT gbg.name GBG, vie.name VIE
    FROM cte_vie vie LEFT JOIN cte_gbg gbg
    ON vie.rn_vie = gbg.rn_gbg

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT gbg.name GBG, vie.name VIE
FROM cte_gbg gbg LEFT JOIN cte_vie vie
ON vie.' at line 53

那么我怎样才能把其他的cte添加到游戏中呢?

2cmtqfgy

2cmtqfgy1#

那个 union 完全连接的近似值还在吗?这是一种糟糕的方法——在功能上不完全相同,而且比必要的成本要高得多。
对于您的问题,使用 union all 和聚合。一种方法是:

SELECT MAX(CASE WHEN which = 'lnz' THEN name END) as lnz,
       MAX(CASE WHEN which = 'wbg' THEN name END) as wbg,
       . . .
FROM ((SELECT 'lnz' as which, rn, lnz.name
       FROM cte_lnz lnz 
      ) UNION ALL
      (SELECT 'wbg', rn, wbg.name
       FROM cte_wbg wbg
      ) UNION ALL
      . . .
     ) n
GROUP BY rn;

添加更多CTE与添加更多CTE一样简单 UNION ALL 子查询中的子句,然后添加更多 MAX(CASE) 外部查询中的表达式。

相关问题