簡體   English   中英

MySQL完全連接三張表

[英]MySQL Full Join Three Tables

有一個問答討論系統,其中包含三個表格,用於三個級別的問答。 表3(三級注釋: GPcommentsFollowup2 )與表2(第二級注釋: GPcommentsFollowup )相關,而表2與表1(第一級注釋: GPcomments )相關。 第2級和第3級實際上是第1級的后續注釋。我想提取每個用戶的貢獻,並顯示用戶在哪個級別貢獻的樹。 例如:

L1-- Title:
|--L2-- Title:
    |--L3-- Title:
    |--L3-- Title:
|--L2-- Title:
    (no L3 contributed)
|--L2-- Title:
    |--L3-- Title:
    |--L3-- Title:
    |--L3-- Title:
L1-- Title:
    (no L2 and L3 contributed)

我嘗試了下面的代碼,但返回值為空。 有什么建議嗎?

function userGroupContributions($pdo, $topicGroupId, $userId1){
$query= 'SELECT L1.commentTitle AS L1T, L1.commentId AS L1Id, L2.commentTitle AS L2T, L2.commentFollowupId AS L2Id, L3.commentTitle AS L3T, L3.commentFollowup2Id AS L3Id FROM GPcomments AS L1
        FULL OUTER JOIN GPcommentsFollowup AS L2 ON L1.commentId = L2.commentId
        FULL OUTER JOIN GPcommentsFollowup2 AS L3 ON L2.commentFollowupId = L3.commentFollowupId
        WHERE (L1.userId=? OR L2.userId=? OR L3.userId=?) AND topicSessionId=?';
$stmt = $pdo->prepare($query);
$stmt->execute(array($userId1, $userId1, $userId1, $topicGroupId));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $L1Id = $row['L1Id'];
    $L2Id = $row['L2Id'];
    $L3Id = $row['L3Id'];
    $L1T = htmlspecialchars_decode($row['L1T']);
    $L2T = htmlspecialchars_decode($row['L2T']);
    $L2T = htmlspecialchars_decode($row['L2T']);

    $data .= '<div>'.$L1T.'</div>';
}
return $data;

}

我已經用下面的代碼解決了:

$query= 'SELECT L1.commentTitle AS L1T, L1.commentId AS L1Id, L1.commentsTypeId AS L1Type, L2.commentTitle AS L2T, L2.commentFollowupId AS L2Id, L2.commentsTypeId AS L2Type, L3.commentTitle AS L3T, L3.commentFollowup2Id AS L3Id, L3.commentsTypeId AS L3Type FROM (SELECT * FROM GPcomments ORDER BY submitDate DESC) AS L1
        LEFT OUTER JOIN (SELECT * FROM GPcommentsFollowup ORDER BY submitDate DESC) AS L2 ON L1.commentId = L2.commentId
        LEFT OUTER JOIN (SELECT * FROM GPcommentsFollowup2 ORDER BY submitDate DESC) AS L3 ON L2.commentFollowupId = L3.commentFollowupId
        WHERE (L1.userId=? AND L1.topicSessionId=?) OR (L2.userId=? AND L2.topicSessionId=?) OR (L3.userId=? AND L3.topicSessionId=?)
        UNION
        SELECT L1.commentTitle AS L1T, L1.commentId AS L1Id, L1.commentsTypeId AS L1Type, L2.commentTitle AS L2T, L2.commentFollowupId AS L2Id, L2.commentsTypeId AS L2Type, L3.commentTitle AS L3T, L3.commentFollowup2Id AS L3Id, L3.commentsTypeId AS L3Type FROM (SELECT * FROM GPcomments ORDER BY submitDate DESC) AS L1
        RIGHT OUTER JOIN (SELECT * FROM GPcommentsFollowup ORDER BY submitDate DESC) AS L2 ON L1.commentId = L2.commentId
        RIGHT OUTER JOIN (SELECT * FROM GPcommentsFollowup2 ORDER BY submitDate DESC) AS L3 ON L2.commentFollowupId = L3.commentFollowupId
        WHERE (L1.userId=? AND L1.topicSessionId=?) OR (L2.userId=? AND L2.topicSessionId=?) OR (L3.userId=? AND L3.topicSessionId=?)';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM