How do you use the “WITH” clause in MySQL?

前端 未结 7 1270
耶瑟儿~
耶瑟儿~ 2020-11-22 04:02

I am converting all my SQL Server queries to MySQL and my queries that have WITH in them are all failing. Here\'s an example:

WITH t1 AS
(
              


        
7条回答
  •  無奈伤痛
    2020-11-22 04:48

    That feature is called a common table expression http://msdn.microsoft.com/en-us/library/ms190766.aspx

    You won't be able to do the exact thing in mySQL, the easiest thing would to probably make a view that mirrors that CTE and just select from the view. You can do it with subqueries, but that will perform really poorly. If you run into any CTEs that do recursion, I don't know how you'd be able to recreate that without using stored procedures.

    EDIT: As I said in my comment, that example you posted has no need for a CTE, so you must have simplified it for the question since it can be just written as

    SELECT article.*, userinfo.*, category.* FROM question
         INNER JOIN userinfo ON userinfo.user_userid=article.article_ownerid
         INNER JOIN category ON article.article_categoryid=category.catid
         WHERE article.article_isdeleted = 0
     ORDER BY article_date DESC Limit 1, 3
    

提交回复
热议问题