简体   繁体   English

大型查询JOINS中的SQL子查询链

[英]Chain of SQL subqueries within large query of JOINS

I am trying to structure a SQL query with complex nested select operation! 我正在尝试使用复杂的嵌套选择操作来构造SQL查询!

My original SQL query successfully JOINS around 30 tables, the data are retrieved as wanted! 我原来的SQL查询成功加入了约30个表,可以按需检索数据! However every fetched record in the 30 tables has many records in another table called (Comments)! 但是,这30个表中的每个获取的记录在另一个表中(注释)都有很多记录! What I want to do is to atribute every record in the (Comments table) to its record in the other 30 tables by IDs and retrieve them all together in one query. 我想做的是按ID将(Comments表)中的每条记录分配给其他30表中的记录,并在一个查询中将它们全部检索到。 Yet this is not the only challenge, some of the 30 tables have in addition to the records in (Comments table) more records in another table called (extras), so i am looking for additional subquery within the main subquery within a LEFT JOIN inside the outter main query. 但这不是唯一的挑战,除了(Comments表)中的记录外,这30个表中的一些还有另一个称为(extras)的表中的更多记录,因此我正在LEFT JOIN内的主子查询中寻找其他子查询外部主要查询。

To make the idea more clear; 使想法更清晰; without subquery the script will be as following: 没有子查询的脚本将如下所示:

$query = $mysqli->query("
SELECT 
parent1.parent1_id,
parent1.child1_id,
parent1.child2_id,
parent1.child3_id,
parent2.parent2_id,
parent2.child1_id,
parent2.child2_id,
parent2.child3_id,
child1.child1_id, 
child1.child1_content,
child2.child2_id, 
child2.child2_content,  
child3.child3_id, 
child3.child3_content
    FROM 
        parent1
    LEFT JOIN child1
        ON child1.child1_id = parent1.child1_id

    LEFT JOIN child2
        ON child2.child2_id = parent1.child2_id

    LEFT JOIN child3
        ON child3.child3_id = parent1.child3_id

    LEFT JOIN followers
        ON parent1.user_id = followers.followed_id
        AND parent1.parent1_timestamp > followers.followed_timestamp
        AND parent1.parent1_id NOT IN (SELECT removed.isub_rmv FROM removed)
        AND parent1.parent1_hide = false
    WHERE 
        followers.follower_id = {$_SESSION['info']}
        {$portname_clause}
    ORDER BY 
        parent1.parent1_timestamp DESC
    LIMIT 
        {$postnumbers} 
    OFFSET 
        {$offset}
")

// Now fetching and looping through the retrieved data

while($row = $query->fetch_assoc()){

    echo $row['child1_content'];
    $subquery1 = $mysqli->query("SELECT extras.child1_id, 
    extras.extrasContent FROM extras WHERE extras.child1_id = 
   {$row['child1_id']}");
    while($row1 = $subquery1->fetch_assoc()){
        echo $row1['extrasContent'];
    }
    echo $row['child2_content'];
    $subquery2 = $mysqli->query("SELECT extras.child2_id, 
    extras.extrasContent FROM extras WHERE extras.child2_id = 
    {$row['child2_id']}");
    while($row2 = $subquery2->fetch_assoc()){
        echo $row2['extrasContent'];
     }

    echo $row['child3_content'];
    $subquery3 = $mysqli->query("SELECT extras.child3_id, 
    extras.extrasContent FROM extras WHERE extras.child3_id = 
    {$row['child3_id']}");
    while($row3 = $subquery3->fetch_assoc()){
        echo $row3['extrasContent'];

        // Here i need to run additional query inside the subquery 3 to retrieve the (Comments table) data beside (extras table)

        $subquery4 = $mysqli->query("SELECT comments.comment_id, comments.comment FROM comments WHERE comments.child3_id = {$row['child3_id']} OR comments.child3_id = {$row3['child3_id']}");
        while($row4 = $subquery4->fetch_assoc()){
        echo $row4['comment'];
        }

    }

} // No sane person would make such code

Because the code above would be totally rediclious i searched for a better way to carry it out, and thats where i came across the subquery concept, but i do not know anything about subqueries, and shortly after i studied it i came up with this messy code, check it below! 因为上面的代码完全是多余的,所以我寻找了一种更好的方法来执行它,这就是我遇到子查询概念的地方,但是我对子查询一无所知,并且在研究了它之后不久,我想到了这个混乱的地方代码,请在下面检查!

I am not posting the origianl code here because it is too long, i am including a virtual example of the tables i want to apply the query on in order to demonstrate the process. 我不在这里发布origianl代码,因为它太长了,我在其中包含了要对其应用查询以演示过程的表的虚拟示例。

SELECT 
parent1.parent1_id,
parent1.child1_id,
parent1.child2_id,
parent1.child3_id,
parent2.parent2_id,
parent2.child1_id,
parent2.child2_id,
parent2.child3_id
FROM 
    parent1
    LEFT JOIN 
        ( SELECT 
        child1.child1_id, 
        child1.child1_content
    FROM 
        child1 
    WHERE 
        child1.child1_id = parent1.child1_id ) child1
        ( SELECT extras.extrasID, extras.extrasContent
    FROM 
        extras
    WHERE 
        extras.child1_id = child1.child1_id )
        ON parent1.child1_id = child1.child1_id
    LEFT JOIN child2
        ( SELECT 
        child2.child2_id, 
        child2.child2_content
        FROM 
           child2 
        WHERE
            child2.child2_id = parent1.child2_id )
            ( SELECT 
            extras.extrasID, 
            extras.extrasContent
            FROM 
                extras
            WHERE 
                extras.child2_id = child2.child2_id )
                    ON parent1.child2_id = child2.child2_id
                LEFT JOIN child3
                    ( SELECT 
                    child3.child3_id, 
                    child3.child3_content
                FROM 
                    child3
                WHERE 
                    child3.child3_id = parent1.child3_id )
                    ( SELECT 
                        extras.extrasID, 
                        extras.extrasContent 
                    FROM 
                        ( SELECT 
                            comments.comment_id, 
                            comments.comment
                        FROM 
                            comments 
                        WHERE 
                            comments.child3_id = extras.child3_id ) extras
                        JOIN child3 
                             ON extras.child3_id = child3.child3_id )

          ON parent1.child3_id = child3.child3_id
   LEFT JOIN followers
        ON parent1.user_id = followers.followed_id
        AND parent1.parent1_timestamp > followers.follower_timestamp
        AND parent1.parent1_id NOT IN (SELECT removed.isub_rmv FROM removed)
        AND parent1.parent1_hide = false
   WHERE 
        followers.follower_id = {$_SESSION['info']}
        {$portname_clause}
   ORDER BY 
       parent1.parent1_timestamp DESC
   LIMIT 
       {$postnumbers} 
   OFFSET
       {$offset} // <-- Sorry for the bad code formatting!

I am using MySql 5.6.37 我正在使用MySql 5.6.37

I did not get the hang of the subquery concept yet, frankly i got lost and confused as i was studying it and for another reason too mentioned in the note below. 我还没有掌握子查询的概念,坦率地说,我在学习它时迷失了自己,感到困惑,并且由于下面的注释中也提到了另一个原因。

Note: I apologize in advance that i might not be on instant reply because where i live there is no electrecity or ADSL or phones and my USB modem hardly gets signal, i have only two hours of averege three hours a day of electericity generated by a desil generator. 注意:我事先表示歉意,因为我住的地方没有电,ADSL或电话,而且我的USB调制解调器几乎没有信号,所以我可能不会立即得到答复,我每天只有两个小时的平均电耗是三个小时Desil发生器。 i recharge my laptop and check internet and the remaining one-two hours are for other life stuff. 我给笔记本电脑充电并检查互联网,剩下的一两个小时用于其他生活。 I know the joke is on me as i am developing a web project without electricity or permanent internet. 我知道我在开玩笑,因为我正在开发一个没有电力或永久性互联网的Web项目。 BUT LIFE DOES NOT GIVE EVERYTHING! 但是生活并不能提供一切! lol. 大声笑。

This is how i solved the problem! 这就是我解决问题的方法!

  SELECT 
        parent1.parent1_id,
        parent1.child1_id,
        child1.child1_id,
        child1.child1_content,
        comments.comment_id, 
        comments.comment, 
        comments.child1_id
  FROM parent1 LEFT JOIN 
(
    SELECT comments.comment_id, comments.comment, comments.child1_id
      FROM 
    (
        SELECT comments.comment_id,comments. comment, comments.child1_id
          FROM comments
    ) comments JOIN child1
        ON comments.child1_id = child1.child1_id
) comments
    ON child1.child1_id = comments.

It needs some aliases and then it's good to go. 它需要一些别名,然后就可以了。

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

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