如何使用“WITH” MySQL 中的子句?
我正在将所有 SQL Server 查询转换为 MySQL,其中包含 WITH
的查询全部失败。这是一个例子:
WITH t1 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
)
SELECT t1.*
FROM t1
ORDER BY t1.article_date DESC
LIMIT 1, 3
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
(
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
)
SELECT t1.*
FROM t1
ORDER BY t1.article_date DESC
LIMIT 1, 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
8.0版本之前的MySQL不支持WITH子句(SQL Server 中的 CTE;Oracle 中的子查询因子分解),因此您只能使用:
对该功能的请求可以追溯到 2006 年。
如前所述,您提供了一个糟糕的示例 - 如果您不以任何方式更改列的输出,则无需执行子选择:
这是一个更好的示例:
MySQL prior to version 8.0 doesn't support the WITH clause (CTE in SQL Server parlance; Subquery Factoring in Oracle), so you are left with using:
The request for the feature dates back to 2006.
As mentioned, you provided a poor example - there's no need to perform a subselect if you aren't altering the output of the columns in any way:
Here's a better example:
Mysql开发团队宣布8.0版本将有 MySQL 中的通用表表达式 (CTE)。因此可以编写如下查询:
Mysql Developers Team announced that version 8.0 will have Common Table Expressions in MySQL (CTEs). So it will be possible to write queries like this:
在 Sql 中,with 语句指定一个临时命名结果集,称为公共表表达式 (CTE)。它可用于递归查询,但在本例中,它指定为子集。如果 mysql 允许子选择我会尝试
In Sql the with statement specifies a temporary named result set, known as a common table expression (CTE). It can be used for recursive queries, but in this case, it specifies as subset. If mysql allows for subselectes i would try
我按照 Lisachenko 分享的链接找到了该博客的另一个链接:
http://guilhembichot.blogspot.co.uk/2013 /11/with-recursive-and-mysql.html
这篇文章列出了模拟 SQL WITH 的两种用法的方法。关于这些如何执行与 SQL WITH 类似的查询的非常好的解释。
1) 使用WITH,这样您就不必多次执行相同的子查询
2) 可以使用存储过程来完成递归查询,该存储过程使调用类似于递归with 查询。
这是代码或存储过程
I followed the link shared by lisachenko and found another link to this blog:
http://guilhembichot.blogspot.co.uk/2013/11/with-recursive-and-mysql.html
The post lays out ways of emulating the 2 uses of SQL WITH. Really good explanation on how these work to do a similar query as SQL WITH.
1) Use WITH so you don't have to perform the same sub query multiple times
2) Recursive queries can be done with a stored procedure that makes the call similar to a recursive with query.
And this is the code or the stored procedure
MySQL中没有“通用表表达式”功能,所以必须去制作视图或临时表来解决,这里我使用了临时表。
这里提到的存储过程将解决您的需求。如果我想获取所有团队成员及其关联成员,此存储过程将有所帮助:
代码:
可以使用以下方式调用:
'Common Table Expression' feature is not available in MySQL, so you have to go to make a view or temporary table to solve, here I have used a temporary table.
The stored procedure mentioned here will solve your need. If I want to get all my team members and their associated members, this stored procedure will help:
Code:
This can be called using:
该功能称为公用表表达式
http://msdn.microsoft.com/en-us/library/ms190766。 aspx
您将无法在 mySQL 中执行完全相同的操作,最简单的操作可能是创建一个镜像该 CTE 的视图,然后从该视图中进行选择。您可以使用子查询来完成此操作,但效果会很差。如果您遇到任何进行递归的 CTE,我不知道如何在不使用存储过程的情况下重新创建它。
编辑:
正如我在评论中所说,您发布的示例不需要 CTE,因此您必须针对问题简化它,因为它可以写为
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
我喜欢@Brad在此线程中的回答,但想要一种方法来保存结果进一步处理(MySql 8):
产生:
I liked @Brad's answer from this thread, but wanted a way to save the results for further processing (MySql 8):
Which produces: