I am trying to create a page that displays all replies to first tier comments of the active user. Here is an example DB table similar to the one I have...
commentid | postid | commid | username | userid | commtext |
---|---|---|---|---|---|
52 | 100 | 0 | user1 | 1 | first |
53 | 100 | 52 | user2 | 2 | second |
54 | 100 | 52 | user1 | 1 | third |
"commentid" is the comment's unique id, "postid" is the id of the post it was on, "commid" gives the number of the comment that it was a response to ("0" indicates it is on the first tier), and so on...
If I am logged in as "user1" and I do the following...
$ra = $DB->Execute('SELECT * FROM comments WHERE userid = '.$DB->qstr($id).' ORDER BY commentid DESC')
...it will return comments #52 and #54. Simple enough.
I am having an issue creating a request to the DB that will output a list that gives me #53 (which was a reply to user1's comment). If possible, I do not want to include #54 which was written by the active user him/herself.
What would be a good way to construct this query?
If you just want to retrieve the first level replies (ignoring those from the initial user), you can JOIN
comments to itself to find the replies, filtering out those with the same userid
:
SELECT rep.*
FROM comments rep
JOIN comments cmt
WHERE rep.commid = cmt.commentid
AND cmt.userid = 1
AND rep.userid != 1
Output (for your sample data):
commentid postid commid username userid commtext
53 100 52 user2 2 second
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.