转《SQL2000论坛上的1个问题。》
原题:MEMBER表中存储所有用户数据,每个注册用户都会有一个tuijianid这个字段,也就是他的上级推荐ID,如何能够筛选出来【没有推荐】过的用户?
比如
ID tuijianid
vip1 vip0
vip2 vip1
vip3 vip2
vip4 vip0
vip5 vip3
vip6 vip5
vip7 vip6
vip8 vip7
vip9 vip2
vip10 vip9
那么这些数据里,只有vip4和vip8没有推荐任何人,如何能够筛选出来呢?
VFP9中做法
CREATE CURSOR member(id c(12),tuijianid c(12))
INSERT INTO member(id,tuijianid) values('vip1','vip0')
INSERT INTO member(id,tuijianid) values('vip2','vip1')
INSERT INTO member(id,tuijianid) values('vip3','vip2')
INSERT INTO member(id,tuijianid) values('vip4','vip0')
INSERT INTO member(id,tuijianid) values('vip5','vip3')
INSERT INTO member(id,tuijianid) values('vip6','vip5')
INSERT INTO member(id,tuijianid) values('vip7','vip6')
INSERT INTO member(id,tuijianid) values('vip8','vip7')
INSERT INTO member(id,tuijianid) values('vip9','vip2')
INSERT INTO member(id,tuijianid) values('vip10','vip9')
**大家注意了,问题来了。
**方法1:
select distinct RTRIM(tuijianid) as tjid from member INTO CURSOR aa
select ID from member where rtrim(ID) NOT in (SELECT tjid FROM aa)
**结果是:vip4,vip8,vip10
select ID from member where RTRIM(ID) not in (select distinct RTRIM(tuijianid) as tjid from member)
**方法2:
**结果是:vip4,vip8,,,,为什么vip10不出现在这?VPF9