I have read over 10 related posts here and elsewhere and still can't figure this one out, being rather new to php and MySQL:
I am using WordPress and trying to implement nested comments within an old theme. My table is all set (let's call it 'comments') and has the following fields:
comment_ID | comment_date | comment_parent, etc.
comment_parent remains equal to 0 in top level comments; it is equal to the comment_ID of the comment replied to in nested replies.
The original MySQL query looks like this:
SELECT * FROM $wpdb->comments
WHERE comment_post_ID = %d AND comment_approved = '1'
ORDER BY comment_date ASC LIMIT %d"
Through the php that follows and outputs the comment list, comments are listed by date without respecting nested replies as such:
comment_ID | comment_date | comment_parent
100 Jan 01 0 (this is a top level comment)
104 Jan 03 0 (this is a top level comment)
106 Jan 04 100 (this is a reply to the first comment)
108 Jan 05 104 (this is a reply to the second comment)
Obviously, the sequence is broken since I am sorting by date. Comment 106 should appear right below comment 100, and comment 108 should be below comment 104.
I am trying not to change my php and I would like to do this with the MySQL query but can't get it right. I have tried using JOIN, GROUP BY, HAVING as suggested in similar questions, without any success. Is there a way for me to achieve the correct sorting right from the query and keep my php intact?