1. 程式人生 > >SQL查詢朋友關係圈應用例項

SQL查詢朋友關係圈應用例項

--------------------------------------------------------------------------

--Author : htl258(Tony)

--Date: 2010-04-14 06:02:36

--Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)

--Jul9 2008 14:43:34

--Copyright (c) 1988-2008 Microsoft Corporation

--Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)

--------------------------------------------------------------------------

--> 生成測試資料表:tb

IFNOTOBJECT_ID('[tb]')ISNULL

DROPTABLE[tb]

GO

CREATETABLE[tb]([callno]INT,[calledno]INT,[groupid]INT)

INSERT[tb]

SELECT 111,1000,1 UNIONALL

SELECT 111,2000,1 UNIONALL

SELECT 222,1000,2 UNIONALL

SELECT 222,4000,2 UNION

ALL

SELECT 333,5000,3 UNIONALL

SELECT 333,6000,3 UNIONALL

SELECT 444,4000,4 UNIONALL

SELECT 444,1,4 UNIONALL

SELECT 444,2,4 UNIONALL

SELECT 555,55,5 UNIONALL

SELECT 555,5000,5 UNIONALL

--

SELECT 666,8,6 UNIONALL

SELECT 666,88,6 UNIONALL

SELECT 666,888,6 UNIONALL

SELECT 777,9,7 UNIONALL

SELECT 777,99,7 UNIONALL

SELECT

777,999,7 UNIONALL

SELECT 888,44,8 UNIONALL

SELECT 888,444,8 UNIONALL

SELECT 999,66,9 UNIONALL

SELECT 999,666,9 UNIONALL

SELECT 999,44,9 UNIONALL

SELECT 999,99,9 UNIONALL

SELECT 9999,44,10 UNIONALL

SELECT 9999,8,10 UNIONALL

--

SELECT 1,100,100 UNIONALL

SELECT 1,200,100 UNIONALL

SELECT 2,200,200 UNIONALL

SELECT 2,300,200 UNIONALL

SELECT 3,300,300 UNIONALL

SELECT 3,400,300 UNIONALL

SELECT 4,400,400 UNIONALL

SELECT 4,500,400 UNIONALL

SELECT 5,500,500 UNIONALL

SELECT 5,600,500 UNIONALL

SELECT 6,600,600

GO

--SELECT * FROM [tb]

-->SQL查詢如下:

DECLARE@CALLNOINT,@CALLEDNOINT

DECLARECCURSORFOR

SELECTCALLNO,CALLEDNOFROMT

OPENC

FETCHNEXTFROMCINTO@CALLNO,@CALLEDNO

WHILE@@FETCH_STATUS= 0

BEGIN

UPDATE T SET

GROUPID=(SELECT MIN(GROUPID) FROM T WHERE CALLEDNO=@CALLEDNO)

WHERE CALLNO=@CALLNO

FETCH NEXT FROM C INTO @CALLNO,@CALLEDNO

END

CLOSEC

DEALLOCATEC

SELECT*FROMT

/*

callnocallednogroupid

----------- ----------- -----------

11110001

11120001

22210001

22240001

33350003

33360003

44440001

44411

44421

555553

55550003

66686

666886

6668886

77797

777997

7779997

888448

8884448

999667

9996667

999447

999997

9999446

999986

1100100

1200100

2200100

2300100

3300100

3400100

4400100

4500100

5500100

5600100

6600100

(36 行受影響)

*/



--溝溝的程式碼:
declare@groupidint, @rowcountintset@groupid=-1whileexists(select1from T where groupid>@groupid)
beginselecttop1@groupid= groupid  fromwhere groupid>@groupidorderby groupid
 
 
set@rowcount=@@rowcountWhile@rowcount>0BEGINUpdate T
 
set groupid=@groupidwhere calledno in(select calledno from T as A where A.groupid=@groupid)
 
and groupid<>@groupidset@rowcount=@@rowcountUpdate T
 
set groupid=@groupidwhere callno in (select callno from T as A where A.groupid=@groupid)
 
and groupid<>@groupidset@rowcount=@rowcount+@@rowcountENDendselect*from T
/*
111    1000    1
111    2000    1
222    1000    1
222    4000    1
333    5000    3
333    6000    3
444    4000    1
444    1    1
444    2    1
555    55    3
555    5000    3

666    8    6
666    88    6
666    888    6
777    9    6
777    99    6
777    999    6
888    44    6
888    444    6
999    66    6
999    666    6
999    44    6
999    99    6
9999    44    6
9999    8    6
1    100    100
1    200    100
2    200    100
2    300    100
3    300    100
3    400    100
4    400    100
4    500    100
5    500    100
5    600    100
6    600    100

*/Droptable T