使用一个SQL语句

初级yl 2010-01-16 07:06:10
题目要求

我的好友被称为 一度好友

好友的好友被称为 二度好友

查询出你的二度好友有多少(使用一个sql语句?)

分析


张三(我的好友):李四,王五。


李四(好友的好友):赵六,周琪,王五

王五(好友的好友):李四,网吧。


张三的二度好友:

赵六,周琪,网吧


数据字典:

会员表:
id会员编号,username会员名称

好友表:
id自动编号,会员编号,好友编号
1 1 2
2 1 3

...全文
116 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
SQL77 2010-01-16
  • 打赏
  • 举报
回复
在MSSQL版,搜索BOM结构
xray2005 2010-01-16
  • 打赏
  • 举报
回复



select t.username

from

(

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

)
ouc_ajax 2010-01-16
  • 打赏
  • 举报
回复
不知道楼主的数据是什么类型?
如果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而且需要详细说明
我会继续回复
lxm200607060125 2010-01-16
  • 打赏
  • 举报
回复
会员表:
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 = '张三')



geniusatm4 2010-01-16
  • 打赏
  • 举报
回复
select distinct t03.name
from {
select 好友表.好友编号
from 好友表
where 会员编号 = 搂住编号
} T01,
好友表 T02
会员表 T03
where T02.好友编号 in T01
andT02.好友编号 = T03.会员编号



maomao90 2010-01-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wangan2008 的回复:]
.....把好友id拼接好 后查出他们的好友id再拼接原来好友的id in一下
[/Quote]
帮顶
wangan2008 2010-01-16
  • 打赏
  • 举报
回复
.....把好友id拼接好 后查出他们的好友id再拼接原来好友的id in一下
sito_hongta 2010-01-16
  • 打赏
  • 举报
回复
不明白需求,UP~
初级yl 2010-01-16
  • 打赏
  • 举报
回复
等着解决呢,谁知道的话,帮我一下!!

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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