上面的结果:
create table a
(
ID int,
thename varchar(10),
FID int
)
insert a
select 1, 'aa', 0 union all
select 2, 'bb', 1 union all
select 3, 'bb', 1 union all
select 4, 'cc', 1 union all
select 5, 'dd', 0 union all
select 6, 'ee', 1 union all
select 7, 'ff', 5
select * from a
select a1.thename as pname,a2.thename,count(*) as cnt from a a1,a a2
where a1.fid=0 and a2.fid=a1.id
group by a1.thename,a2.thename
select max(dname) as dname,max(thename) as thename,count(*) as coun from
(select a1.thename as dname,a.thename from a, a a1 where a.fid=a1.id) b
group by dname,thename
select a.thename as dname,abc.thename,count(*) as coun
from abc,(select * from abc) a where abc.fid=a.id group by a.thename,abc.thename
对于这个呢,a是一个表别名 a为select * from abc的结果,当然,其实没必要这样的
select max(dname) as dname,max(thename) as thename,count(*) as coun from
(select a.thename as dname,abc.thename from abc,(select * from abc) a where abc.fid=a.id) b
group by dname,thename