任何其他改变方式" With As" for mariadb

时间:2015-09-18 02:38:52

标签: sql-server database union mariadb recursive-query

WITH KPILibHier (kpilib_code,parent_code,kpi_name, depth, iscategory)
AS ( SELECT K.kpilib_code, K.parent_code, K.kpi_name_en, K.kpi_depth, K.iscategory 
     FROM TPMDPERIODKPILIB K 
     INNER JOIN TPMDPERIODKPI PK ON PK.period_code = K.period_code 
     UNION ALL 
     SELECT A.kpilib_code, A.parent_code, A.kpi_name_en, A.kpi_depth, A.iscategory 
     FROM TPMDPERIODKPILIB A 
     INNER JOIN KPILibHier AS B ON A.kpilib_code = B.parent_code )
SELECT DISTINCT Z.kpi_name AS libname, Z.kpilib_code AS libcode,
Z.parent_code AS pcode, Z.depth, Z.iscategory, PK.target
FROM KPILibHier Z
LEFT JOIN TPMDPERIODKPILIB KPI 
  ON KPI.kpilib_code = Z.kpilib_code
LEFT JOIN TPMDPERIODKPI PK 
  ON PK.period_code = KPI.period_code 
ORDER BY Z.depth, Z.kpi_name 

这是我的代码。 我可以在SQL Server中运行它, 但现在我必须使用Mariadb, 而Mariadb并没有使用" WITH AS"。 那么有没有其他方法可以为同样的结果更改此代码?

正如我们所看到的,那里有" INNER JOIN KPILibHier"在CTE里面, 所以我不能像往常一样制作普通的子查询。

1 个答案:

答案 0 :(得分:0)

只是将CTE的内容移动到派生表应该提供可用的东西。喜欢的东西;

SELECT DISTINCT Z.kpi_name AS libname, Z.kpilib_code AS libcode,
Z.parent_code AS pcode, Z.depth, Z.iscategory, PK.target
FROM
( SELECT K.kpilib_code, K.parent_code, K.kpi_name_en AS kpi_name, K.kpi_depth AS depth, K.iscategory 
     FROM TPMDPERIODKPILIB K 
     INNER JOIN TPMDPERIODKPI PK ON PK.period_code = K.period_code 
     UNION ALL 
     SELECT A.kpilib_code, A.parent_code, A.kpi_name_en, A.kpi_depth, A.iscategory 
     FROM TPMDPERIODKPILIB A 
     ) z
INNER JOIN z AS B ON A.kpilib_code = B.parent_code 
LEFT JOIN TPMDPERIODKPILIB KPI 
  ON KPI.kpilib_code = Z.kpilib_code
LEFT JOIN TPMDPERIODKPI PK 
  ON PK.period_code = KPI.period_code 
ORDER BY Z.depth, Z.kpi_name