php - How can i get records of id those are connected in multiple row value

Following is my table
{-code-1}
I want to output a comment reply structure, which can be described by an example. On the table above you can see user_id1
post comment on 833( this is a post id )
a post, thenuser 168
repliy to him as "Great" then after againuser 1
replay to user 168.
So it could be like...
**MY POST** ( This is a post )
---- First comment( Amazing)
------- replay comment (Great)
I have written the following query but I could not get the output that I expect.
SQL Query:
{-code-7}
Answer
Answer
Solution:
If you are running MySQL 8.0, a typical approach is to use a recursive query to traverse the hierarchical data structure. This produces the desired result with a single query, so this is much more efficient that iterating in php.
The anchor of the recursive query selects the "first" comment of the post, which is we identify with condition{-code-2}
. Then, the recursive part just follows the relationships until they exhaust.
This gives you a flat structure that lists all comment's posts, with an addition column named{-code-3}
that indicates the depth of each element. You can use that information to handle the display in your application.
Id | User_id | type | content | item_id | secondary_item_id | {-code-3} --: | ------: | :
Answer
Answer
Solution:
You can use the below code to get your output in SQL.
WITH comment_activity(Id, User_id, type, content, post_id, secondary_item_id)
AS
(Select 879 , 1 , 'activity_comment' , 'Amazing' , 833 , 833 UNION
Select 907 , 168 , 'activity_comment' , 'Great' , 833 , 879 UNION
Select 908 , 1 , 'activity_comment' , 'Welcome' , 833 , 907
)
Select post_id as post,
ROW_NUMBER() OVER (PARTITION BY post_id ORDER BY ID) as RN,
content as comment,
User_id,
CASE WHEN ROW_NUMBER() OVER (PARTITION BY post_id ORDER BY ID) = 1
THEN 'First_Comment (' + content + ')'
ELSE 'Reply_' + CONVERT(VARCHAR(20),ROW_NUMBER() OVER (PARTITION BY post_id ORDER BY ID) - 1) + '( ' + content + ')'
END as Thread
from comment_activity
WHERE type = 'activity_comment'
AND post_id = 833
Answer
Solution:
Actually Lajos Arpad's solution could work with some changes in the db. You add another column parent_item_id. Which is basically the first comment of that chain of comments. In your example it would be 879 for all the 3 rows.
Now this could work
select * from <table name>
where item_id = <item_id>
group by parent_item_id
order by secondary_item_id asc
Answer
Solution:
Let's first understand how we can load the raw data:
select Id, content, item_id, secondary_item_id
from table_one
where item_id = 833
order by item_id, secondary_item_id;
The query above loads the comments on post 833 and orders them by the post id and the parent comment id. So far, so good. Now, assuming that you have the raw data stored in$rawResults
, you can do the following:
$map = [];
$parsedResults = [];
foreach ($rawResults as $rawResult) {
if ($rawResult["secondary_item_id"] === $rawResult["item_id"]) {
$parsedResults[$rawResult["Id"]] = [
"content" => $rawResult["content"],
"children" => []
];
$map[$rawResult["Id"]] = $parsedResults[$rawResults["Id"]];
} else {
$map[$rawResult["secondary_item_id"]]["children"][$rawResult["Id"]]= [
"content" => $rawResult["content"],
"children" => []
]
}
}
Now, you have a hierarchy of elements, the main levels are main comments, to which you can find the answers in the children item. You can now traverse your hierarchical array like
function getComments($parsedResults, $prefix = "--- ") {
$output = "";
foreach ($parsedResults as $parsedResult) {
$output .= $prefix.$parsedResult["content"]."\n";
if (count($parsedResult["children"])) $output .= getComments($parsedResult["children"], "---".$prefix);
}
return $output;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: err_ossl_pem_no_start_line
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.