| 发表于:2007-09-10 16:06:433楼 得分:20 |
create table tb(id int,parentid int) insert into tb values(1, 0) insert into tb values(2, 1) insert into tb values(3, 1) insert into tb values(4, 0) insert into tb values(5, 4) insert into tb values(6, 0) go select a.id 节点,isnull(b.子点数,0) 子点数 from tb a left join (select parentid,count(*) 子点数 from tb group by parentid) b on a.id = b.parentid drop table tb /* 点 子点数 ----------- ----------- 1 2 2 0 3 0 4 1 5 0 6 0 (所影响的行数为 6 行) */ | | |
|