简体   繁体   English

将mysql子查询转换为左联接

[英]Transform mysql Subquery to Left Join

suppose I have the following table: 假设我有下表:

    id  parent
    1   1
    2   1
    3   1
    4   1
    5   2
    6   5
    7   5

And I want to execute the following query: 我想执行以下查询:

SELECT id_table.parent,id_table.count_id 
FROM 
    (SELECT count(id) AS count_id,parent FROM item group by parent) AS id_table
     WHERE id_table.count_id = 
         (SELECT max(count_id) AS max_count_id FROM
                      ( SELECT count(id) AS count_id,parent
                            FROM item group by parent ) AS id_table2 )

to get the following result: 得到以下结果:

parent  count_id    
1       4

I try to transform the query to optimize performance: 我尝试转换查询以优化性能:

SELECT id_table.parent,id_table.count_id FROM
      (SELECT count(id) AS count_id,parent FROM item GROUP BY parent)
              AS id_table    
LEFT JOIN

      (SELECT count(id) AS count_id,parent FROM item GROUP BY parent )
              AS id_table2    
ON id_table.parent=id_table2.parent AND id_table.count_id<id_table2.count_id

WHERE id_table2.parent IS NULL

BUT got the following result instead, which I don't want: 但是得到了以下结果,我不想要:

parent  count_id    
1       4
2       1   
5       2

This is my reference 这是我的参考

Could anyone tell me where I did wrong? 谁能告诉我我做错了什么? regards 问候

Your second query does not work as you wish because it simply does not match any records in the join. 您的第二个查询无法按您期望的方式工作,因为它根本不匹配联接中的任何记录。 Look: 看:

You are declaring a join between a certain query and itself, according to the following condition: 您正在根据以下条件声明某个查询与其本身之间的联接:

ON id_table.parent=id_table2.parent AND id_table.count_id<id_table2.count_id

This means: select all the records from the first query (because it is a LEFT join), and every record from the second query which has the same parent and a count lower than the first query. 这意味着:从第一个查询中选择所有记录(因为它是LEFT联接),并从第二个查询中选择每个记录,这些记录具有相同的parent并且count低于第一个查询。 ¡! ¡!

As you can see, this is absurd: Since the auto-joined query returns these records: 如您所见,这是荒谬的:由于自动联接的查询返回以下记录:

count_id parent
4        1
2        2
2        5

... the parent column is a de-facto PK. ... parent列是实际 PK。 So. 所以。 the pair (4,1) has no matching pairs (x,1) with x<4. 该对(4,1)没有x <4的匹配对(x,1)。 (Idem the rest of the pairs.) (将其余两对视为同一个。)

That was your mistake: The example from MySQL is not appliable here, because in the shop table, surely there are more than one record with the same value of article , which is not your case of ( parent has unique values in the auto-joined query). 那是您的错误: MySQL的示例在这里不适用,因为在shop表中,肯定有多个记录具有相同的article值,这与您的情况不同( parent在自动联接中具有唯一值)查询)。

The only query formula based on joins that comes to my mind is like this: 我想到的唯一一个基于联接的查询公式是这样的:

SELECT count_id, parent
FROM (SELECT count(id) AS count_id,parent FROM item GROUP BY parent) query1
INNER JOIN (SELECT MAX(query2.count_id) as max_id FROM (SELECT count(id) AS count_id,parent FROM item GROUP BY parent) query2) queryMax ON query1.count_id=queryMax.max_id;

... but I doubt that such a complexity it will improve the performance of your first approach. ...但是我怀疑这种复杂性是否会提高您第一种方法的性能。

Based on your output which you wanted to get i got a simpler solution. 根据您想要获得的输出,我得到了一个更简单的解决方案。 Maybe i didn't understand your question, but i'll give a shot. 也许我不明白您的问题,但我会给您一个机会。

Why to use your query (kinda long and messy) : 为什么要使用您的查询(有点冗长又杂乱):

SELECT id_table.parent,id_table.count_id 
FROM 
(SELECT count(id) AS count_id,parent FROM item group by parent) AS id_table
 WHERE id_table.count_id = 
     (SELECT max(count_id) AS max_count_id FROM
                  ( SELECT count(id) AS count_id,parent
                        FROM item group by parent ) AS id_table2 )

I acomplished the same result with much more simpler query. 我用更简单的查询完成了相同的结果。

SELECT parent, count(id) as count_id FROM items GROUP BY 1 ORDER BY 2 DESC LIMIT 1

To get the following result. 得到以下结果。

parent  count_id    
1       4

Can you check this 你能检查一下吗

SELECT parent,count(*) AS parent_count FROM parent group by parent
 ORDER BY parent_count DESC LIMIT 1

Hope this helps 希望这可以帮助

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

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