sql语句求解。mysql 系统是三级分销系统,想要一个 按照 分销团队下线数最多的 进行一个排行

2025-02-24 17:52:11
推荐回答(1个)
回答1:

select sum(count) from (
--一级
select count(*) as count from table_name where pid= 'id'
union all
--二级
select count(*) as count from table_name t1
inner join table_name t2
on t1.pid = t2.id
where t2.pid= 'id'
union all
--三级
select count(*) as count from table_name t1
inner join (
select id,pid from table_name t1
inner join table_name t2
on t1.pid = t2.id
where t2.pid= 'id'
) t2 on t1.pid = t2.id
)