Just use order, count and group by… Something like this:
SELECT *, count(*) AS child_count FROM people GROUP BY Father ORDER BY child_count DESC
This will select everything, group it by father creating a child_count of how many children and sort it by the child_count numbers. You then use WHILE() loop to display each Father plus the child_count results. If you need the names of the children, you need to add in a nested WHILE() loop to display those using a second results from a query.
Well, when you run a query, you return a value. Sometimes this “value” is null or just empty. In PHP, you just compare the value and if it is null or empty, display something else. Are you doing this father-of-father in just one query? If so, show us and I can tell you how to do alternative results in a query. ( You just have to use “IF IS NULL” condition and select the value or the substitute. If you are doing this inside your PHP code, you just compare the results. $row or $results whatever… If ($row[“father”]==NULL) { nothing…} or if (trim($row[“father”])=="") { nothing…}
Hope this helps!