1

我需要一个脚本来找到不止一次有朋友联系的会员,这样它就不会在网站上多次显示他们的友谊。

MemberConnection 表如下:

+----------------------+------------+----------------+
|  MemberConnectionID  |  MemberID  |  ConnMemberID  |
+----------------------+------------+----------------+
|  25                  |  33        | 43             |
|  26                  |  43        | 33             |
|  27                  |  13        | 143            |
|  28                  |  143       | 13             |
|  29                  |  33        | 43             |
+----------------------+------------+----------------+

如您所见,第 25 行和第 29 行是相同的,我需要找到这些重复项以便将它们删除。

我该如何为此编写脚本?

任何人都可以提供的任何帮助将不胜感激。

提前谢谢了

新杰克

4

4 回答 4

3

如果您想保留一个连接,请尝试以下操作:

使用它来选择连接:

;WITH CTE
AS
(
   SELECT
      MemberConnectionID, 
      ROW_NUMBER() OVER (PARTITION BY MemberID, ConnMemberID ORDER BY MemberConnectionID) RN
   FROM MemberConnection 
)
SELECT 
      MemberConnectionID
FROM CTE
WHERE RN > 1

或使用它从表中删除:

;WITH CTE
AS
(
   SELECT
      ROW_NUMBER() OVER (PARTITION BY MemberID, ConnMemberID ORDER BY MemberConnectionID) RN
   FROM MemberConnection 
)
DELETE
FROM CTE
WHERE RN > 1
于 2013-02-01T15:56:56.627 回答
0

使用它作为测试:

DECLARE @Table AS TABLE
    (
      MemberConnectionID INT ,
      MemberID INT ,
      ConMemberID INT
    )

INSERT  INTO @Table
        SELECT  1 ,
                2 ,
                3
INSERT  INTO @Table
        SELECT  2 ,
                3 ,
                4
INSERT  INTO @Table
        SELECT  3 ,
                2 ,
                3

选择出现的成员连接

SELECT    COUNT(MemberConnectionID) AS Occurence ,
          MemberID ,
          ConMemberID
FROM      @Table
GROUP BY  MemberID ,
          ConMemberID

删除语句

DELETE  FROM @Table
FROM    @Table t
        INNER JOIN ( SELECT MemberID ,
                            ConMemberID
                     FROM   ( SELECT    COUNT(MemberConnectionID) AS Occurence ,
                                        MemberID ,
                                        ConMemberID
                              FROM      @Table
                              GROUP BY  MemberID ,
                                        ConMemberID
                            ) t2
                     WHERE  t2.Occurence > 1
                   ) t3 ON t3.MemberID = T.MemberID
                           AND t3.ConMemberID = t.ConMemberID
于 2013-02-01T16:06:25.527 回答
0

我总是使用这个小查询(更改为满足您的表)。

Delete MemberConnection
from MemberConnection
join
(select max(MemberConnectionId)as CountIt, MemberId, ConnMemberId
    from #MemberConnection
    group by MemberId, ConnMemberId
    having count(1) > 1
) as derived
on MemberConnection.MemberId = derived.MemberId
and MemberConnection.ConnMemberId = derived.ConnMemberId
and CountIt > MemberConnectionId
于 2013-02-01T16:07:05.660 回答
0

您可以执行以下操作:

SELECT * FROM 
(SELECT MemberID,ConnMemberID  ,count( MemberID,ConnMemberID ) as c FROM MemberConnection group by MemberID,ConnMemberID ) x 
WHERE x.c >1

这将显示所有重复的行。

于 2013-02-01T15:56:25.930 回答