题目要求 我的好友被称为 一度好友 好友的好友被称为 二度好友 查询出你的二度好友有多少(使用一个sql语句?)分析
张三(我的好友):李四,王五。
李四(好友的好友):赵六,周琪,王五 王五(好友的好友):李四,网吧。
张三的二度好友:
赵六,周琪,网吧
数据字典:会员表:
id会员编号,username会员名称好友表:
id自动编号,会员编号,好友编号
1 1 2
2 1 3
张三(我的好友):李四,王五。
李四(好友的好友):赵六,周琪,王五 王五(好友的好友):李四,网吧。
张三的二度好友:
赵六,周琪,网吧
数据字典:会员表:
id会员编号,username会员名称好友表:
id自动编号,会员编号,好友编号
1 1 2
2 1 3
from {
select 好友表.好友编号
from 好友表
where 会员编号 = 搂住编号
} T01,
好友表 T02
会员表 T03
where T02.好友编号 in T01
andT02.好友编号 = T03.会员编号
id会员编号,username会员名称 好友表:
id自动编号,会员编号,好友编号
1 1 2
2 1 3 先来吧好友选出来
select username from 会员表 where 会员id in (select 好友编号 from 好友表 where 会员编号 = (select 会员id from 会员表 where username = 本人))
在选出好友的好友select count(friendid) from friend
where userid in (
select userid from [user]
where userid in (select friendid from friend
where userid = (select userid from [user] where username = '张三'))) and not in (
select userid from [user] where username = '张三')
如果oracle很好实现!
示例:
假如有如下结构的表:some_table(id,p_id,name),其中p_id保存父记录的id。
select * from some_table t connect by prior t.p_id=t.id start with t.id = 123这样就是全部链接的数据。那么count(*)就是全部好友!
如果是sqlserver的话自己查询出来DataTable然后排序吧。
采用xml节点的那种形式,如果是sqlserver而且需要详细说明
我会继续回复
select t.usernamefrom (select u.id,u.username
from user u,friend f
where u.id=f.id and u.id in
(
select u1.userId
from user u1,friend f1
where u1.Id=f1.Id and u1.id=我的ID
)) t
where t.id not in (
select u1.userId
from user u1,friend f1
where u1.Id=f1.Id and u1.id=我的ID)