By default, parent_id
= 0. I want to select all records with parent_id
= 0 and only the last ones with parent_id
> 0.
I tried this, but it didn't work:
SELECT * FROM `articles`
IF `parent_id` > 0 THEN
GROUP BY `parent_id`
HAVING COUNT(`parent_id`) >= 1
END;
ORDER BY `time` DESC
I mean, that if there are a few records with parent_id = 2, only one of them should be return. Also, if there a number of records with parent_id = 5, only one of them is returned. In other words, there should no more than one record of each parent_id apart from those having parent_id = 0.
What could be the solution?