JOINS

时间:2018-02-21 18:06:17

标签: mysql sql nested

我正在尝试使用复杂的嵌套选择操作构建SQL查询!

我的原始SQL查询成功加入了大约30个表,数据被按原样检索!但是每张30张表中的记录 在另一个名为(评论)的表中有很多记录!我想要做的是将(评论表)中的每个记录归属到另一个记录 按ID分列30个表,并在一个查询中一起检索它们。然而,这不是唯一的挑战,30张桌子中的一些除了记录之外还有 (注释表)另一个名为(extras)的表中的更多记录,所以我在主子查询中寻找额外的子查询 在主要主查询中的LEFT JOIN。

使想法更清晰;如果没有子查询,脚本将如下所示:

$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

因为上面的代码完全是rediclious,我搜索了一个更好的方法来执行它,那就是我遇到子查询的地方 概念,但我对子查询一无所知,在我研究之后不久,我想出了这个混乱的代码,请在下面查看!

我不是在这里发布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!

我正在使用MySql 5.6.37

我还没有得到子查询概念的悬念,坦率地说,当我正在研究它时,我迷路了,并且在下面的注释中提到了另一个原因。

注意:我提前道歉,我可能不会立即回复,因为我居住的地方没有电子邮件或ADSL或电话和我的 USB调制解调器几乎没有信号,我每天只需要两个小时的时间来完成一个由desil发电机产生的电能。我充电 我的笔记本电脑和互联网检查,剩下的一两个小时是其他生活的东西。 我知道这个笑话在我身上,因为我正在开发一个没有电力或永久互联网的网络项目。但生活不给所有的一切!洛尔。

1 个答案:

答案 0 :(得分:0)

这就是我解决问题的方法!

  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.

它需要一些别名,然后它就好了。