I can not seem to get the first query to return the results in 'dateAdded' order. I am after the most recent result in each group to be returned.
It seems that the grouping is done in a different way than I expected it to be done in.
I guess what I am after is the same as my second query, but them to have them grouped after the ORDER BY `c`.`dateAdded` DESC not before, which is what I think is happening?
Code:
SELECT `c`.`id`, `c`.`dateAdded`, `c`.`itemid`, `c`.`comment`,
`i`.`userid` AS `itemOwner`, `i`.`title`,
`c`.`userid` AS `posterId`, `poster`.`fullname` AS `posterName`
FROM `comments` as `c`
LEFT JOIN `items` as `i` ON `i`.`itemid` = `c`.`itemid`
LEFT JOIN `users` as `poster` ON `poster`.`userid` = `c`.`userid`
WHERE `c`.`userid` = '" . mysql_real_escape_string($_SESSION['user']['userid']) . "'
GROUP BY `c`.`itemid`
ORDER BY `c`.`dateAdded` DESC
The second one returns the results as expected, in dateAdded order.
Code:
SELECT `c`.`id`, `c`.`dateAdded`, `c`.`itemid`, `c`.`comment`,
`i`.`userid` AS `itemOwner`, `i`.`title`,
`c`.`userid` AS `posterId`, `poster`.`fullname` AS `posterName`
FROM `comments` as `c`
LEFT JOIN `items` as `i` ON `i`.`itemid` = `c`.`itemid`
LEFT JOIN `users` as `poster` ON `poster`.`userid` = `c`.`userid`
WHERE `c`.`itemid` = '" . $getCommentTitle['itemid'] . "'
ORDER BY `c`.`dateAdded` DESC