22,300
社区成员




CREATE TABLE [dbo].[tb_UserFriend](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NOT NULL,--用户ID
[FriendId] [int] NOT NULL--好友ID
)
GO
select distinct userid
from tb_UserFriend
where Friend in (
select Friend
from tb_UserFriend
where Userid=@userid
) and userid<>@userid
select distinct b.userid
from (
select Friend
from tb_UserFriend
where Userid=@userid
) a
left join tb_UserFriend b
on a.friend=b.friend
where b.userid<>@userid
--建表
CREATE TABLE #tb_UserFriend(
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NOT NULL,--用户ID
[FriendId] [int] NOT NULL--好友ID
)
--数据
Insert into [#tb_UserFriend]([UserId],[FriendId])values(1,3)
Insert into [#tb_UserFriend]([UserId],[FriendId])values(1,4)
Insert into [#tb_UserFriend]([UserId],[FriendId])values(1,5)
Insert into [#tb_UserFriend]([UserId],[FriendId])values(1,6)
Insert into [#tb_UserFriend]([UserId],[FriendId])values(2,4)
Insert into [#tb_UserFriend]([UserId],[FriendId])values(2,5)
Insert into [#tb_UserFriend]([UserId],[FriendId])values(2,6)
Insert into [#tb_UserFriend]([UserId],[FriendId])values(3,5)
Insert into [#tb_UserFriend]([UserId],[FriendId])values(3,6)
declare @UserID int
--当@UserID=2时
set @UserID=2
--结果
select count(0) as [count],b.UserID from #tb_UserFriend as a,#tb_UserFriend as b
where a.friendID=b.friendID
and a.UserID=@UserID and b.UserID<>@UserID
group by b.UserID
--
count UserID
----------- -----------
3 1
2 3
(所影响的行数为 2 行)
select
UserId,count(1)
from
tb_UserFriend a,FriendId b
where
a.FriendId=b.FriendId
and
UserId=@pid
group by
UserId
CREATE TABLE #temp(
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NOT NULL,--用户ID
[FriendId] [int] NOT NULL--好友ID
)
GO
Insert into #temp([UserId],[FriendId])values(1,3);
Insert into #temp([UserId],[FriendId])values(1,4);
Insert into #temp([UserId],[FriendId])values(1,5);
Insert into #temp([UserId],[FriendId])values(1,6);
Insert into #temp([UserId],[FriendId])values(2,4);
Insert into #temp([UserId],[FriendId])values(2,5);
Insert into #temp([UserId],[FriendId])values(2,6);
Insert into #temp([UserId],[FriendId])values(3,5);
Insert into #temp([UserId],[FriendId])values(3,6);
go
--SQL:
declare @UserId varchar(10)
set @UserId = 2
select cnt=COUNT(1), b.UserId
from #temp a inner join #temp b
on a.FriendId = b.FriendId and a.UserId = @UserId and a.UserId <> b.UserId
group by b.UserId
/*
cnt UserId
3 1
2 3
*/
--FriendId加索引
--UserID加索引
declare @pid int
set @pid =2 --变量
select count(1) as [count],UserId
from tb_UserFriend a,(select FriendId from tb_UserFriend where UserId=@pid) b
where a.FriendId=b.FriendId and a.UserId<>@pid
group by UserId
/*
count UserId
----------- -----------
3 1
2 3
*/
CREATE TABLE [dbo].[tb_UserFriend](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NOT NULL,--用户ID
[FriendId] [int] NOT NULL--好友ID
)
GO
Insert into [tb_UserFriend]([UserId],[FriendId])values(1,3);
Insert into [tb_UserFriend]([UserId],[FriendId])values(1,4);
Insert into [tb_UserFriend]([UserId],[FriendId])values(1,5);
Insert into [tb_UserFriend]([UserId],[FriendId])values(1,6);
Insert into [tb_UserFriend]([UserId],[FriendId])values(2,4);
Insert into [tb_UserFriend]([UserId],[FriendId])values(2,5);
Insert into [tb_UserFriend]([UserId],[FriendId])values(2,6);
Insert into [tb_UserFriend]([UserId],[FriendId])values(3,5);
Insert into [tb_UserFriend]([UserId],[FriendId])values(3,6);
select UserId,count(1)
from tb_UserFriend a,(select FriendId from tb_UserFriend where UserId=2) b
where a.FriendId=b.FriendId
group by UserId
/*
UserId
----------- -----------
1 3
2 3
3 2
if object_id('p1') is not null
drop proc p1
go
create proc p1
@userid int
as
begin
select *
from(
select cnt=count(*),t.userid from tb_UserFriend s ,tb_UserFriend t
where s.friendid=t.friendid and s.userid=@userid
group by t.userid)K
where userid!=@userid
end
测试:
exec p1 2
结果:
cnt userid
----------- -----------
3 1
2 3
select count(*),t.userid from tb_UserFriend s ,tb_UserFriend t
where s.friendid=t.friendid
group by t.userid
Insert into [tb_UserFriend]([UserId],[FriendId])values(1,3);
Insert into [tb_UserFriend]([UserId],[FriendId])values(1,4);
Insert into [tb_UserFriend]([UserId],[FriendId])values(1,5);
Insert into [tb_UserFriend]([UserId],[FriendId])values(1,6);
Insert into [tb_UserFriend]([UserId],[FriendId])values(2,4);
Insert into [tb_UserFriend]([UserId],[FriendId])values(2,5);
Insert into [tb_UserFriend]([UserId],[FriendId])values(2,6);
Insert into [tb_UserFriend]([UserId],[FriendId])values(3,5);
Insert into [tb_UserFriend]([UserId],[FriendId])values(3,6);
最好给出完整的表结构,测试数据,计算方法和正确结果.否则耽搁的是你宝贵的时间。
如果有多表,表之间如何关联?