go
with CTE as
(
-->Begin 一个定位点成员
select id,uid,account,parent_id,mobile,email,addtime,integral,cast(account as nvarchar(max)) as TE,0 as Levle from user_table where parent_id =0
-->End
union all
-->Begin一个递归成员
select user_table.id,user_table.uid,user_table.account,user_table.parent_id,user_table.mobile,user_table.email,user_table.addtime,user_table.integral,cast(replicate(' ',len(CTE.TE))+'|_'+user_table.account as nvarchar(MAX)) as TE,Levle+1 as Levle
from user_table inner join CTE
on user_table.parent_id=CTE.uid
-->End
)
select * from CTE order by id desc
id 自增ID
uid 系统编号
account 用户名
parent_id 父子标识
mobile 手机号码
email 邮箱账号
addtime 注册时间
integral 积分
逻辑是这样的,现在根据递归可以查到所有自己下面的会员,由于每一级的会员很多,如何在递归的同时根据每一级会员的 integral 积分最大为判断条件来进行递归下一级会员 ,每一级会员提取一条积分最大的,然后根据这个条件来提取下一级会员,依次类推
------解决思路----------------------
无测试数据,试下。
with CTE as
(
-->Begin 一个定位点成员
select id,uid,account,parent_id,mobile,email,addtime,integral,cast(account as nvarchar(max)) as TE,0 as Levle from user_table where parent_id =0
-->End
union all
-->Begin一个递归成员
select a.id,a.uid,a.account,a.parent_id,a.mobile,a.email,a.addtime,a.integral,cast(replicate(' ',len(CTE.TE))+'
------解决思路----------------------
_'+a.account as nvarchar(MAX)) as TE,Levle+1 as Levle
from user_table a inner join CTE
on a.parent_id=CTE.uid
where not exists(select 1 from user_table where parent_id=a.parent_id and integral>a.integral )
-->End
)
select * from CTE order by id desc