in reply to Hash Ref: Need to find out how many children I have :-)
The actual rows have further more levels. The key importance here is perfomance.Can you put this all into a database? Once there, gets a lot easier (and faster if indexed/structured properly).
If you use that query to construct a temp table called "grandchildren", then your answers become things like:users user_id first_name last_name user_age -- !!!! really should be stored as birthdate or birthyear parent_user_rlt parent_id child_id SELECT grand.user_id as grandparent_id, parent.user_id as parent_id, child.user_id as child_id, child.first_name as child_first, child_last_name as child_last FROM users as grand LEFT JOIN parent_user_rlt as g2p ON g2p.parent_id = grand.user_id LEFT JOIN users as parent ON parent.user_id = g2p.child_id LEFT JOIN parent_user_rlt as p2c ON p2c.parent_id = parent.user_id LEFT JOIN users as child ON child.user_id = g2p.child_id ;
SELECT distinct parent_id FROM grandchildren; SELECT grandparent_id, count(distinct parent_id) FROM grandchildren +GROUP BY grandparent_id; SELECT grandparent_id, parent_id, count(*) as num_kids FROM grandchi +ldren GROUP BY grandparent_id, parent_id; SELECT child_id, child_first, child_last FROM grandchildren;
|
---|