There is a Q/A discussion system with three tables for three level Q/A comments. Table 3 (level three comment: GPcommentsFollowup2
) is related to table 2 (level two comment: GPcommentsFollowup
) and table 2 is related to table 1 (level one comment: GPcomments
). Level 2 and 3 are actually followup comments of Level 1. I want to extract the contributions of each user showing the tree at which level the user contributed. For example:
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)
I tried the code below, but the return value is empty. Is there any suggestion.
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;
}
I have solve it with this code:
$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=?)';