CREATE TABLE `comments` (
`comment_id` int(11) NOT NULL AUTO_INCREMENT,
`comment_parent_id` int(11) NOT NULL DEFAULT '0',
`user_id` int(11) NOT NULL DEFAULT '0',
`comment_text` varchar(200) NOT NULL DEFAULT '',
`comment_created` int(20) NOT NULL DEFAULT '0',
`comment_updated` int(20) NOT NULL DEFAULT '0',
`comment_replies_count` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`comment_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
Each comment can have multiple replies, however, replies cannot be replied to. So when someone replies to a comment, the row they insert will have the ID for the comment they replied to in the parent ID column.
I would like to retrieve all comments and if the comment has replies, I would like to retrieve the last reply.
SELECT c1.*
FROM comments c1
WHERE comment_parent_id = '0'
ORDER BY comment_created DESC;
So if c1.comment_replies_count > 0 I would like to...
SELECT c2.*
FROM comments c2
WHERE comment_parent_id = c1.comment_id
ORDER BY comment_created DESC Limit 1;
Can this be achieved in 1 query? Or Is it best to do another call to the database, during the php loop statement, to fetch the last reply to the comment?
Thanks in advance and please pardon my ignorance as I'm learning still.