如今在web2.0网络时代,自从国外的MySpace,SpaceBook,到以人人网,开心网,海内网为代表交友SNS社区,发展更是火的不得了。依靠六度理论建立的SNS网站发展和盈利必须依靠强大的用户,帮助用户经营扩大朋友圈,经营的越好,吸引来玩的人就越多,社区也就人气越旺。因此,很多就推出了“共同好友”的功能,以此来帮助用户扩大朋友圈,下面说说 此功能的程序逻辑。
<1> SQl编写的逻辑:
代码
Declare @UserID int //当前登陆用户ID
Declare @cFriendCount int //设置共同好友的个数
set @UserID=888 //设置当前登陆用户ID
Declare @cFriendCount int //设置共同好友的个数
set @UserID=888 //设置当前登陆用户ID
set @cFriendCount=1 //设为共同好友个数为 1(大部分网站都设为1个共同好友)
SELECT UserID
FROM Contacter_Friend_Relationship as t1
where FriendUserID in (select FriendUserID from Contacter_Friend_Relationship where UserID=@UserID) and UserID<>@UserID and(select count(*) from Contacter_Friend_Relationship where UserID=t1.UserID and FriendUserID=@UserID)<1
group by UserID having count(UserID)>=@cFriendCount
SELECT UserID
FROM Contacter_Friend_Relationship as t1
where FriendUserID in (select FriendUserID from Contacter_Friend_Relationship where UserID=@UserID) and UserID<>@UserID and(select count(*) from Contacter_Friend_Relationship where UserID=t1.UserID and FriendUserID=@UserID)<1
group by UserID having count(UserID)>=@cFriendCount
<2>Linq To SQL 的编写逻辑:
代码
int curUserId;
from fri in dcontext.Contacter_Friend_Relationship
join f in dcontext.Contacter_Friend_Relationship on fri.FriendUserID equals f.FriendUserID
join f in dcontext.Contacter_Friend_Relationship on fri.FriendUserID equals f.FriendUserID
where f.UserID == curUserId && fri.FriendUserID != curUserId
&& dcontext.Contacter_Friend_Relationship.Count(C=>C.UserID==user.UserID&&C.FriendUserID==curUserId)<1
group fri by fri.UserID into ff
where ff.Count() >= Fricount //Fricount 设置共同好友的个数
select ff
where ff.Count() >= Fricount //Fricount 设置共同好友的个数
select ff
这些只是实现数据库好友关系表中找出的 共同好友的UserID,如果还要得到 改UserID的个人信息呢,还得去连个人信息表查出个人信息!如果隐私设置的话,再连上隐私设置表!