Here is what I came up with, maybe someone else can do better?

SELECT
fam.id,
fam.parent,
IF(f.id IS NOT NULL,f.id,fam.child_id) as child_id,
IF(f.id IS NOT NULL,f.name,fam.child) as child
FROM (
SELECT p.id,p.name as parent,p.favorite,c.id as child_id,c.name as child
FROM parents p
LEFT JOIN children c ON c.parent_id = p.id
ORDER BY c.age
) fam
LEFT JOIN children f ON f.id = fam.favorite
GROUP BY fam.id

--
Ritter

_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to