简体   繁体   English

在mysql中派生表的自联接 - 我需要重复子查询吗?

[英]Self-joins on derived tables in mysql- do i need to repeat the subqueries?

I've got to execute a self-joining sql statement on a derived table in mysql. 我必须在mysql中的派生表上执行自连接的sql语句。 The derived table involves a hairy subquery, and I'm wondering if there's any alternative to actually writing and executing it twice- 派生表涉及一个毛茸茸的子查询,我想知道是否有任何替代实际编写和执行它两次 -

SELECT a.* FROM (my hairy subquery) AS a
  LEFT JOIN (my hairy subquery) AS a2 
    ON a.groupname = a2.groupname etc..

The standard solution to this is to use CTEs, but these are not yet supported in MySQL. 对此的标准解决方案是使用CTE,但MySQL尚不支持这些。 Alternatives are: 替代方案是:

  • You can put your subquery in a view and self-join the view. 您可以将子查询放在视图中并自行加入视图。
  • You can create a temporary table and populate it with the results of your subquery. 您可以创建一个临时表,并使用子查询的结果填充它。

Related 有关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM