[sql]
SELECT t1. * , COUNT( t2.id )
FROM `tree` AS t1, `tree` AS t2
WHERE t1.parent = '' AND t2.parent = t1.id
GROUP BY t1.id;
[/sql]
Это для следующей таблицы:
[sql]
CREATE TABLE tree (
id varchar(10) NOT NULL default '',
parent varchar(10) NOT NULL default '',
name varchar(10) NOT NULL default '',
PRIMARY KEY (id),
KEY parent (parent)
) TYPE=MyISAM;
[/sql]