如何将 MSSQL CTE 查询转换为 MySQL?

How to transform a MSSQL CTE query to MySQL?(如何将 MSSQL CTE 查询转换为 MySQL?)
本文介绍了如何将 MSSQL CTE 查询转换为 MySQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在我的 MySQL 架构中,我有 category(id, parentid, name)

in my MySQL schema, I have the category(id, parentid, name) table

在 MSSQL 中,我有那个 CTE 查询(为提供的类别 ID 自下而上构建类别树:

In the MSSQL, I have that CTE query (to build a category tree from the bottom up for a supplied category ID:

with CTE (id, pid, name) 
as
(
    select id, parentid as pid,name
    from category
    where id = 197
      union all
        select CTE.pid as id , category.parentid as pid, category.name
        from CTE 
          inner join category 
            on category.id = CTE.pid
 )
 select * from CTE 

如何将该查询转换"为 MySQL?

How to 'transform' that query to MySQL ?

推荐答案

遗憾的是 MySQL 不支持 CTE(通用表表达式).这是早就应该的 IMO.通常,您可以只使用子查询,但这个特殊的 CTE 是递归:它在查询中引用自身.递归 CTE 对于分层数据非常有用,但同样:MySql 根本不支持它们.您必须实现一个存储过程才能获得相同的结果.

Unfortunately MySQL doesn't support CTE (Common Table Expressions). This is long overdue IMO. Often, you can just use a subquery instead, but this particular CTE is recursive: it refers to itself inside the query. Recursive CTE's are extremely useful for hierarchical data, but again: MySql doesn't support them at all. You have to implement a stored procedure to get the same results.

我之前的回答应该提供一个很好的起点:

A previous answer of mine should provide a good starting point:

从 MySQL 中的分层数据生成基于深度的树(无 CTE)>

这篇关于如何将 MSSQL CTE 查询转换为 MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)