会员系统的问题

wmlf 2006-01-19 12:25:59
我有这样一个表
TABLE1 字段
ID int
userid nvarchar
gxrid int
数据库中的数据是 1
ID USERID GXRID 2
1 A 0 3
2 B 1 4 5 6
3 C 2 7, 8, 9 10, 11, 12, 13, 14, 15
4 D 3
5 E 3
6 F 3
....................................
会员之间存在着这样的 一个关系图 缛右边============>
会员之间钱面三为是一个一对一的推荐关系
但是 从第3 位开始 一个人对应会有三个人的推荐人
这样无限的推荐下去

现在我给出一个ID 希望求出 这个ID下面没有推荐人和不满三个推荐人的 会员的ID
...全文
138 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
samfeng_2003 2006-01-19
  • 打赏
  • 举报
回复
这样才对,因为最后一层是没有推荐人的。只要这样就可以换算,但是我想我是不是把你的意思理解反了??

select id,userid from dbo.f_he(2) a
where id not in
(select GXRID from dbo.f_he(3) b group by GXRID having count(*)>=3)

id userid
----------- --------------------
2 B
4 D
5 E
6 F
7 G
8 H

(所影响的行数为 6 行)
samfeng_2003 2006-01-19
  • 打赏
  • 举报
回复
create table t
(id int,userid nvarchar(20),gxrid int)

insert t
select 1,'A',0 union all
select 2,'B',1 union all
select 3,'C',2 union all
select 4,'D',3 union all
select 5,'E',3 union all
select 6,'F',3 union all
select 7,'G',4 union all
select 8,'H',4
GO
create function f_he(@id int)
returns @re table(id int,userid nvarchar(20),GXRID int,flag int)
as
begin
declare @i int
set @i=0
insert into @re
select *,0 from t where id=@id
while @@rowcount>0
begin
set @i=@i+1
insert into @re
select a.*,@i from t a,@re b where a.GXRID=b.id and b.flag=@i-1
end
return
end
go

select id,userid from dbo.f_he(1) a
where GXRID not in
(
select GXRID from dbo.f_he(1) b group by GXRID having count(*)>=3
)
and id not in
(select GXRID from dbo.f_he(1) b group by GXRID having count(*)>=3)


drop function f_he
drop table t

id userid
----------- --------------------
1 A
2 B
7 G
8 H

(所影响的行数为 4 行)
-神仙- 2006-01-19
  • 打赏
  • 举报
回复
select GXRID from table1 group by GXRID having count(GXRID)<3;
lw1a2 2006-01-19
  • 打赏
  • 举报
回复
参考:

http://community.csdn.net/Expert/topic/4521/4521837.xml?temp=.4002497

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧