-1

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?

2
  • Does something like this work 'SELECT * FROM comments WHERE commid = 52 AND userid != '.$DB->qstr($id).' ORDER BY commentid DESC'?
    – RJK
    Feb 4, 2021 at 20:41
  • Something like that would be on the right track, but instead of saying "commid = 52", I would need to implement some way to lookup that number based on the userid.
    – banjax
    Feb 4, 2021 at 21:08

1 Answer 1

0

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

Demo on db-fiddle

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.