0

用户数据

    UserId  DisplayName
12  Rahul
13  Anjali
14  Faruk
15  Shabina
16  Shakira
17  Ali
18  Ronak
19  Dali
20  Bali
21  Kali
22  Katrina
23  Sita
24  Gita
25  Ram
26  Shyam
27  Suhana
28  Suhas
29  Raj
30  Taslim
31  Ritik
32  Tejas
33  Dipika
34  Bush
35  Dyna
36  Bushiar
37  Salman
38  Ruksana
39  Khushi
40  Tazz
41  Miki
42  Krish
43  Kumbh

家谱数据

ID  UserID  RelativeId  Relation
1   12  13  Spouse
3   12  15  Daughter
4   12  16  Daughter
5   12  17  Son
6   12  18  Son
7   13  12  Spouse
9   13  15  Daughter
10  13  16  Daughter
11  13  17  Son
12  13  18  Son
13  14  15  Spouse
14  14  20  Son
15  14  21  Daughter
16  14  19  Daughter
17  15  12  Father
18  15  13  Mother
19  15  14  Spouse
20  15  16  Sister
21  15  18  Brother
22  15  20  Son
23  15  19  Daughter
24  15  21  Daughter
25  16  12  Father
26  16  13  Mother
27  16  17  Spouse
28  16  14  Brother
29  16  18  Brother
30  16  23  Daughter
31  16  24  Daughter
32  16  25  Son
33  16  26  Son
34  17  16  Spouse
35  17  23  Daughter
36  17  24  Daughter
37  17  25  Son
38  17  26  Son
39  18  12  Father
40  18  13  Mother
41  18  19  Spouse
42  18  27  Daughter
43  18  28  Son
44  18  29  Son
45  18  15  Sister
46  18  16  Sister
47  19  14  Father
48  19  15  Mother
49  19  18  Spouse
50  19  27  Daughter
51  19  28  Son
52  19  29  Son
53  19  20  Brother
54  19  21  Sister
55  20  14  Father
56  20  15  Mother
57  20  21  Sister
58  20  19  Sister
59  20  40  Spouse
60  20  42  Son
61  20  43  Son
62  21  14  Father
63  21  15  Mother
64  21  25  Spouse
65  21  40  Daughter
66  21  41  Son
67  21  20  Brother
68  21  19  Sister
102 22  28  Spouse
103 22  31  Son
69  23  16  Mother
70  23  17  Father
71  23  24  Sister
72  23  25  Brother
73  23  26  Brother
74  24  16  Mother
75  24  17  Father
76  24  23  Sister
77  24  25  Brother
78  24  26  Brother
79  25  16  Mother
80  25  17  Father
81  25  23  Sister
82  25  24  Sister
83  25  26  Brother
84  25  21  Spouse
85  25  40  Daughter
86  25  41  Son
87  26  16  Mother
88  26  17  Father
89  26  23  Sister
90  26  24  Sister
91  26  25  Brother
92  26  27  Spouse
93  26  34  Son
94  26  31  Daughter
95  27  18  Father
96  27  19  Mother
97  27  26  Spouse
98  27  28  Brother
99  27  29  Brother
100 27  34  Son
101 27  35  Daughter
104 28  18  Father
105 28  19  Mother
106 28  27  Sister
107 28  29  Brother
108 28  22  Spouse
109 28  31  Son
110 29  18  Father
111 29  19  Mother
112 29  27  Sister
113 29  28  Brother
114 29  30  Spouse
115 29  32  Son
116 30  29  Spouse
117 30  32  Son
118 31  28  Father
119 31  22  Mother
120 31  34  Spouse
121 31  36  Son
122 32  29  Father
123 32  30  Mother
124 32  33  Spouse
125 32  35  Spouse
126 32  37  Son
127 32  38  Daughter
128 33  32  Spouse
129 33  38  Daughter
130 34  26  Father
131 34  27  Mother
132 34  35  Sister
133 34  31  Spouse
134 34  36  Son
135 35  26  Father
136 35  27  Mother
137 35  32  Spouse
138 35  34  Brother
139 35  37  Son
140 36  34  Father
141 36  31  Father
142 37  35  Mother
143 37  32  Father
144 37  38  Spouse
145 37  39  Daughter
146 38  32  Father
147 38  33  Mother
148 38  37  Spouse
149 38  39  Daughter
150 39  37  Father
151 39  38  Mother
152 40  21  Mother
153 40  25  Father
154 40  41  Brother
155 40  20  Spouse
156 40  42  Son
157 40  43  Son
158 41  21  Mother
159 41  25  Father
160 41  40  Sister
161 42  20  Father
162 42  40  Mother
163 42  43  Brother
164 43  20  Father
165 43  40  Mother
166 43  42  Brother

想找到找到Rahul(Id = 12)和Tejas(Id = 32)之间关系的路径,谢谢提前帮助......

上述数据图表: 替代文字

asstander提供的查询:

DECLARE @Users TABLE(
        UserID INT,
        UserName VARCHAR(50)
)

DECLARE @FamilyTree TABLE(
        UserID INT,
        RelationID INT,
        Relation VARCHAR(50)
)



INSERT INTO @Users (UserID,UserName) SELECT 1 ,'Rahul'
INSERT INTO @Users (UserID,UserName) SELECT 2 ,'Anjli'
INSERT INTO @Users (UserID,UserName) SELECT 3 ,'Ronak'
INSERT INTO @Users (UserID,UserName) SELECT 4 ,'Rutu'
INSERT INTO @Users (UserID,UserName) SELECT 5 ,'Riya'
INSERT INTO @Users (UserID,UserName) SELECT 6 ,'Rishi'
INSERT INTO @Users (UserID,UserName) SELECT 7 ,'Harry'
INSERT INTO @Users (UserID,UserName) SELECT 8 ,'Dyna'

INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 1, 2 ,'Wife'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 2, 1 ,'Rahul'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 1, 3 ,'Son'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 3, 4 ,'Wife'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 3, 5 ,'Daughter'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 4, 5 ,'Daughter'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 4, 6 ,'Son'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 3, 6 ,'Son'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 1, 7 ,'Son'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 2, 7 ,'Son'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 7, 8 ,'Wife'

DECLARE @UserID1 INT,
        @UserID2 INT

SELECT  @UserID1 = 1,
        @UserID2 = 6


DECLARE @Table TABLE(
        cnt INT,
        cpath VARCHAR(50)
)

;WITH fam AS(
        SELECT  *,
                        1 AS CntLevel,
                        CAST('/' + CAST(ft.UserID AS VARCHAR(10)) + '/' AS VARCHAR(50)) AS CLevel
        FROM    @FamilyTree ft 
        WHERE   ft.UserID = @UserID1
        UNION ALL
        SELECT  ft.*,
                        fam.CntLevel + 1 AS CntLevel,
                        CAST(fam.Clevel  + CAST(ft.UserID AS VARCHAR(10)) + '/'AS VARCHAR(50)) AS CLevel
        FROM    @FamilyTree ft INNER JOIN
                        fam ON ft.UserID = fam.RelationID
        WHERE   ft.UserID != @UserID1
)
INSERT INTO @Table(cnt, cpath)
SELECT  CntLevel,
        CAST(fam.Clevel  + CAST(fam.RelationID AS VARCHAR(10)) + '/'AS VARCHAR(50))
FROM    fam
WHERE   CAST(fam.Clevel  + CAST(fam.RelationID AS VARCHAR(10)) + '/'AS VARCHAR(50)) LIKE ('/' + CAST(@UserID1 AS VARCHAR(10)) + '/%')
AND     CAST(fam.Clevel  + CAST(fam.RelationID AS VARCHAR(10)) + '/'AS VARCHAR(50)) LIKE ('%/' + CAST(@UserID2 AS VARCHAR(10))+ '/')

SELECT  DISTINCT 
        ft.* 
FROM    @Table t INNER JOIN
        @FamilyTree ft  ON      PATINDEX('%/' + CAST(ft.UserID AS VARCHAR(10)) + '/%',t.cpath) > 0
                                        AND PATINDEX('%/' + CAST(ft.RelationID AS VARCHAR(10)) + '/%',t.cpath) > 0
4

3 回答 3

1

你可以试试这样的

DECLARE @Users TABLE(
        UserID INT,
        UserName VARCHAR(50)
)

DECLARE @FamilyTree TABLE(
        UserID INT,
        RelationID INT,
        Relation VARCHAR(50)
)



INSERT INTO @Users (UserID,UserName) SELECT 1 ,'Rahul'
INSERT INTO @Users (UserID,UserName) SELECT 2 ,'Anjli'
INSERT INTO @Users (UserID,UserName) SELECT 3 ,'Ronak'
INSERT INTO @Users (UserID,UserName) SELECT 4 ,'Rutu'
INSERT INTO @Users (UserID,UserName) SELECT 5 ,'Riya'
INSERT INTO @Users (UserID,UserName) SELECT 6 ,'Rishi'
INSERT INTO @Users (UserID,UserName) SELECT 7 ,'Harry'
INSERT INTO @Users (UserID,UserName) SELECT 8 ,'Dyna'

INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 1, 2 ,'Wife'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 2, 1 ,'Rahul'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 1, 3 ,'Son'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 3, 4 ,'Wife'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 3, 5 ,'Daughter'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 4, 5 ,'Daughter'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 4, 6 ,'Son'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 3, 6 ,'Son'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 1, 7 ,'Son'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 2, 7 ,'Son'
INSERT INTO @FamilyTree (UserID,RelationID,Relation) SELECT 7, 8 ,'Wife'

DECLARE @UserID1 INT,
        @UserID2 INT

SELECT  @UserID1 = 1,
        @UserID2 = 6


DECLARE @Table TABLE(
        cnt INT,
        cpath VARCHAR(50)
)

;WITH fam AS(
        SELECT  *,
                1 AS CntLevel,
                CAST('/' + CAST(ft.UserID AS VARCHAR(10)) + '/' AS VARCHAR(50)) AS CLevel
        FROM    @FamilyTree ft 
        WHERE   ft.UserID = @UserID1
        UNION ALL
        SELECT  ft.*,
                fam.CntLevel + 1 AS CntLevel,
                CAST(fam.Clevel  + CAST(ft.UserID AS VARCHAR(10)) + '/'AS VARCHAR(50)) AS CLevel
        FROM    @FamilyTree ft INNER JOIN
                fam ON ft.UserID = fam.RelationID
        WHERE   ft.UserID != @UserID1
)
INSERT INTO @Table(cnt, cpath)
SELECT  CntLevel,
        CAST(fam.Clevel  + CAST(fam.RelationID AS VARCHAR(10)) + '/'AS VARCHAR(50))
FROM    fam
WHERE   CAST(fam.Clevel  + CAST(fam.RelationID AS VARCHAR(10)) + '/'AS VARCHAR(50)) LIKE ('/' + CAST(@UserID1 AS VARCHAR(10)) + '/%')
AND     CAST(fam.Clevel  + CAST(fam.RelationID AS VARCHAR(10)) + '/'AS VARCHAR(50)) LIKE ('%/' + CAST(@UserID2 AS VARCHAR(10))+ '/')

SELECT  DISTINCT 
        ft.* 
FROM    @Table t INNER JOIN
        @FamilyTree ft  ON  PATINDEX('%/' + CAST(ft.UserID AS VARCHAR(10)) + '/%',t.cpath) > 0
                        AND PATINDEX('%/' + CAST(ft.RelationID AS VARCHAR(10)) + '/%',t.cpath) > 0
于 2009-11-23T07:07:26.020 回答
1

嗯,复杂的家庭。试试这个。使用最终输出进行自定义。

声明@用户表(
        用户 ID INT,
        用户名 VARCHAR(50)
)

声明 @FamilyTree 表([ID] int,
        用户 ID INT,
         相对 ID INT,
        关系 VARCHAR(50)
)



插入@Users
选择 12,'Rahul' 联盟
选择 13,'Anjali' 联盟
选择 14,'Faruk' 联盟
选择 15,'Shabina' 联盟
选择16,'夏奇拉'联盟
选择17,'阿里'联盟
选择 18,'Ronak' 联盟
选择19,'大理'联盟
选择20,'巴厘岛'联盟
选择 21,'Kali' 联盟
选择 22,“卡特里娜”联盟
选择 23,'Sita' 联合
选择 24,'Gita' 联合
选择 25,'Ram' 联合
选择 26,'Shyam' 联合
选择 27,'Suhana' 联盟
选择 28,'Suhas' 联盟
选择 29,'Raj' 联合
选择 30,'Taslim' 联合
选择 31,'Ritik' 联合
选择 32,'光辉'联盟
选择 33,'Dipika' 联合
选择 34,'布什'工会
选择 35,'Dyna' 联合
选择 36,'Bushiar' 工会
选择 37,“萨尔曼”联盟
选择 38,'Ruksana' 联盟
选择 39,'Khushi' 联盟
选择 40,'Tazz' 联盟
选择41,'三木'工会
选择 42,'Krish' 联盟
选择 43,'Kumbh'




插入@FamilyTree([ID],UserID,RelativeId,Relation)
选择 1,12,13,'配偶'工会
选择 3,12,15,'女儿'工会
选择 4,12,16,'女儿'工会
选择 5,12,17,'Son' union
选择 6,12,18,'Son' union
选择 7,13,12,'配偶'工会
选择 9,13,15,'女儿'工会
选择 10,13,16,'女儿'工会
选择 11,13,17,'Son' union
选择 12,13,18,'Son' union
选择 13,14,15,'配偶'工会
选择 14,14,20,'Son' union
选择 15,14,21,'女儿'工会
选择 16,14,19,'女儿'工会
选择 17,15,12,'父亲'工会
选择 18,15,13,​​'母亲'工会
选择 19,15,14,'配偶'工会
选择 20,15,16,'姐妹'工会
选择 21,15,18,'兄弟'工会
选择 22,15,20,'Son' union
选择 23,15,19,'女儿'工会
选择 24,15,21,'女儿'工会
选择 25,16,12,'父亲'工会
选择 26,16,13,'母亲'工会
选择 27,16,17,'配偶'工会
选择 28,16,14,‘兄弟’工会
选择 29,16,18,'兄弟'联盟
选择 30,16,23,'女儿'工会
选择 31,16,24,'女儿'工会
选择 32,16,25,'Son' union
选择 33,16,26,'Son' union
选择 34,17,16,'配偶'工会
选择 35,17,23,'女儿'工会
选择 36,17,24,'女儿'工会
选择 37,17,25,'Son' union
选择 38,17,26,'Son' union
选择 39,18,12,'父亲'工会
选择 40,18,13,'母亲'工会
选择 41,18,19,'配偶'工会
选择 42,18,27,'女儿'工会
选择 43,18,28,'Son' union
选择 44,18,29,'Son' union
选择 45,18,15,'姐妹'工会
选择 46,18,16,'姐妹'工会
选择 47,19,14,'父亲'工会
选择 48,19,15,'母亲'工会
选择 49,19,18,'配偶'工会
选择 50,19,27,'女儿'工会
选择 51,19,28,'Son' union
选择 52,19,29,'Son' union
选择 53,19,20,'兄弟'联盟
选择 54,19,21,'姐妹'工会
选择 55,20,14,'父亲'工会
选择 56,20,15,'母亲'工会
选择 57,20,21,'姐妹'工会
选择 58,20,19,'姐妹'工会
选择 59,20,40,'配偶'工会
选择 60,20,42,'Son' union
选择 61,20,43,'Son' union
选择 62,21,14,'父亲'工会
选择 63,21,15,'母亲'工会
选择 64,21,25,'配偶'工会
选择 65,21,40,'女儿'工会
选择 66,21,41,'Son' union
选择 67,21,20,'兄弟'联盟
选择 68,21,19,'姐妹'工会
选择 102,22,28,'配偶'工会
选择 103,22,31,'Son' union
选择 69,23,16,'母亲'工会
选择 70,23,17,'父亲'工会
选择 71,23,24,'姐妹'工会
选择 72,23,25,'兄弟'联盟
选择 73,23,26,'兄弟'工会
选择 74,24,16,'母亲'工会
选择 75,24,17,'父亲'工会
选择 76,24,23,'姐妹'工会
选择 77,24,25,'兄弟'联盟
选择 78,24,26,'兄弟'联盟
选择 79,25,16,'母亲'工会
选择 80,25,17,'父亲'工会
选择 81,25,23,'姐妹'工会
选择 82,25,24,'姐妹'工会
选择 83,25,26,'兄弟'联盟
选择 84,25,21,'配偶'工会
选择 85,25,40,'女儿'工会
选择 86,25,41,'Son' union
选择 87,26,16,'母亲'工会
选择 88,26,17,'父亲'工会
选择 89,26,23,'姐妹'工会
选择 90,26,24,'姐妹'工会
选择 91,26,25,'兄弟'工会
选择 92,26,27,'配偶'工会
选择 93,26,34,'Son' union
选择 94,26,31,'女儿'工会
选择 95,27,18,'父亲'工会
选择 96,27,19,'母亲'工会
选择 97,27,26,'配偶'工会
选择 98,27,28,'兄弟'联盟
选择 99,27,29,'兄弟'联盟
选择 100,27,34,'Son' union
选择 101,27,35,'女儿'工会
选择 104,28,18,'父亲'工会
选择 105,28,19,'母亲'工会
选择 106,28,27,'姐妹'工会
选择 107,28,29,'兄弟'联盟
选择 108,28,22,'配偶'工会
选择 109,28,31,'Son' union
选择 110,29,18,'父亲'工会
选择 111,29,19,'母亲'工会
选择 112,29,27,'姐妹'工会
选择 113,29,28,'兄弟'联盟
选择 114,29,30,'配偶'工会
选择 115,29,32,'Son' union
选择 116,30,29,'配偶'工会
选择 117,30,32,'Son' union
选择 118,31,28,'父亲'工会
选择 119,31,22,'母亲'工会
选择 120,31,34,'配偶'工会
选择 121,31,36,'Son' union
选择 122,32,29,'父亲'工会
选择 123,32,30,'母亲'工会
选择 124,32,33,'配偶'工会
选择 125,32,35,'配偶'工会
选择 126,32,37,'Son' union
选择 127,32,38,'女儿'工会
选择 128,33,32,'配偶'工会
选择 129,33,38,'女儿'工会
选择 130,34,26,'父亲'工会
选择 131,34,27,'母亲'工会
选择 132,34,35,'姐妹'工会
选择 133,34,31,'配偶'工会
选择 134,34,36,'Son' union
选择 135,35,26,'父亲'工会
选择 136,35,27,'母亲'工会
选择 137,35,32,'配偶'工会
选择 138,35,34,'兄弟'工会
选择 139,35,37,'Son' union
选择 140,36,34,'父亲'工会
选择 141,36,31,'父亲'工会
选择 142,37,35,'母亲'工会
选择 143,37,32,'父亲'工会
选择 144,37,38,'配偶'工会
选择 145,37,39,'女儿'工会
选择 146,38,32,'父亲'工会
选择 147,38,33,'母亲'工会
选择 148,38,37,'配偶'工会
选择 149,38,39,'女儿'工会
选择 150,39,37,'父亲'工会
选择 151,39,38,'母亲'工会
选择 152,40,21,'母亲'工会
选择 153,40,25,'父亲'工会
选择 154,40,41,'兄弟'联盟
选择 155,40,20,'配偶'工会
选择 156,40,42,'Son' union
选择 157,40,43,'Son' union
选择 158,41,21,'母亲'工会
选择 159,41,25,'父亲'工会
选择 160,41,40,'姐妹'工会
选择 161,42,20,'父亲'工会
选择 162,42,40,'母亲'工会
选择 163,42,43,'兄弟'联盟
选择 164,43,20,'父亲'工会
选择 165,43,40,'母亲'工会
选择 166,43,42,'兄弟'  




声明@UserID1 INT,
        @UserID2 INT

选择@UserID1 = 12,
        @用户 ID2 = 32

--user1的后代
;与famDes
作为
(
    选择 * ,
        cast(ltrim(userid) as varchar(max)) as [path],
        转换(关系为 varchar(max))为 [path2]
    来自@家谱
    其中 UserID=@UserID1 和 Relation in ('Son','Daughter')

    联合所有
    选择英尺*,
        cast([path]+'.'+ltrim(ft.userid) as varchar(max)),
        cast([path2]+'.'+ft.relation as varchar(max))
    来自@FamilyTree英尺
    ft.UserID=famDes.RelativeId 和 ft.Relation in ('Son','Daughter') 上的内部连接 ​​famDes

),
--user1的后代
家庭成员
作为
(
    选择 * ,
        cast(ltrim(userid) as varchar(max)) as [path],
        转换(关系为 varchar(max))为 [path2]
    来自@家谱
    其中 UserID=@UserID1 和 ('Father','Mother') 中的关系

    联合所有
    选择英尺*,
        cast([path]+'.'+ltrim(ft.userid) as varchar(max)),
        cast([path2]+'.'+ft.relation as varchar(max))
    来自@FamilyTree英尺
    ft.UserID=famAsc.RelativeId 和 ft.Relation in ('Father','Mother') 上的内部连接 ​​famAsc

)
选择
    *
从
    家族
在哪里  
    相对 ID=@UserID2
联合所有
选择
    *
从
    家庭成员
在哪里  
    相对 ID=@UserID2
联合所有
--如果 user1 和 user2 是兄弟或配偶
选择
    * ,
    cast(ltrim(userid) as varchar(max)) as [path],
    转换(关系为 varchar(max))为 [path2]
    来自@家谱
在哪里
    (@UserID1,@UserID2) 中的用户 ID
    和('兄弟','姐妹','配偶')中的关系
    和 (@UserID2 ,@UserID1 ) 中的 relativeID


于 2009-11-23T19:14:08.577 回答
0

我从那里找到了一篇好文章,我可以 为该文章创建查询链接

询问 :

DECLARE @From_Node int,

        @To_Node   int

SET @From_Node = 100035

SET @To_Node   = 100046



SELECT *,

       edges = 5 - ( CASE WHEN p2 = -1 THEN 4 WHEN p3 = -1 THEN 3 WHEN p4 = -1 THEN 2 WHEN p5 = -1 THEN 1 ELSE 0 END )

FROM (

    SELECT p0 = p1.From_Node,

           p1 = COALESCE( p1.To_Node, -1 ),

           p2 = COALESCE( p2.To_Node, -1 ),

           p3 = COALESCE( p3.To_Node, -1 ),

           p4 = COALESCE( p4.To_Node, -1 ),

           p5 = COALESCE( p5.To_Node, -1 )

    FROM paths AS p1

        OUTER APPLY tvfn_get_nodes_for( p1.To_Node, @From_Node, @To_Node ) AS p2

        OUTER APPLY tvfn_get_nodes_for( p2.To_Node, @From_Node, @To_Node ) AS p3

        OUTER APPLY tvfn_get_nodes_for( p3.To_Node, @From_Node, @To_Node ) AS p4

        OUTER APPLY tvfn_get_nodes_for( p4.To_Node, @From_Node, @To_Node ) AS p5

    WHERE p1.From_Node = @From_Node

    ) AS d

WHERE ( p1 = @To_Node OR p2 = @To_Node OR p3 = @To_Node OR p4 = @To_Node OR p5 = @To_Node )

  AND ( p1 NOT IN ( p2, p3, p4, p5 )

     OR p1 = -1 )

  AND ( p2 NOT IN ( p1, p3, p4, p5 )

     OR p2 = -1 )

  AND ( p3 NOT IN ( p1, p2, p4, p5 )

     OR p3 = -1 )

  AND ( p4 NOT IN ( p1, p2, p3, p5 )

     OR p4 = -1 )

  AND ( p5 NOT IN ( p1, p2, p3, p4 )

     OR p5 = -1 )

ORDER BY edges
于 2009-11-23T13:58:36.937 回答