将SQL表与自身进行比较(自联接)

时间:2009-12-11 19:40:08

标签: sql join self-join

我正在尝试根据混合列找到重复的行。这是我的一个例子:

CREATE TABLE Test
(
   id INT PRIMARY KEY,
   test1 varchar(124),
   test2 varchar(124)
)

INSERT INTO TEST ( id, test1, test2 ) VALUES ( 1, 'A', 'B' )
INSERT INTO TEST ( id, test1, test2 ) VALUES ( 2, 'B', 'C' )

现在,如果我运行此查询:

SELECT [LEFT].[ID] 
FROM [TEST] AS [LEFT] 
   INNER JOIN [TEST] AS [RIGHT] 
   ON [LEFT].[ID] != [RIGHT].[ID] 
WHERE [LEFT].[TEST1] = [RIGHT].[TEST2]

我希望能找回两个id。 (1和2),但我只回到了一行。

我的想法是它应该比较每一行,但我想这不正确? 为了解决这个问题,我将查询更改为:

SELECT [LEFT].[ID] 
FROM [TEST] AS [LEFT] 
   INNER JOIN [TEST] AS [RIGHT] 
   ON [LEFT].[ID] != [RIGHT].[ID] 
WHERE [LEFT].[TEST1] = [RIGHT].[TEST2] 
OR [LEFT].[TEST2] = [RIGHT].[TEST1]

这给了我两行,但性能会根据行数极快地降低。

我为性能和结果而提出的最终解决方案是使用联合:

SELECT [LEFT].[ID] 
FROM [TEST] AS [LEFT] 
   INNER JOIN [TEST] AS [RIGHT] 
   ON [LEFT].[ID] != [RIGHT].[ID] 
WHERE [LEFT].[TEST1] = [RIGHT].[TEST2] 
UNION
SELECT [LEFT].[ID] 
FROM [TEST] AS [LEFT] 
   INNER JOIN [TEST] AS [RIGHT] 
   ON [LEFT].[ID] != [RIGHT].[ID] 
WHERE [LEFT].[TEST2] = [RIGHT].[TEST1]

但总的来说,我显然不理解为什么这不起作用,这意味着我可能做错了什么。有人能指出我正确的方向吗?

4 个答案:

答案 0 :(得分:11)

不要加入不平等;似乎JOIN和WHERE条件被反转。

SELECT t1.id
FROM Test t1
INNER JOIN Test t2
ON ((t1.test1 = t2.test2) OR (t1.test2 = t2.test1))
WHERE t1.id <> t2.id

应该可以正常工作。

答案 1 :(得分:5)

如果你选择它们,你只能取回两个id:

SELECT [LEFT].[ID], [RIGHT].[ID] 
FROM [TEST] AS [LEFT] 
   INNER JOIN [TEST] AS [RIGHT] 
   ON [LEFT].[ID] != [RIGHT].[ID] 
WHERE [LEFT].[TEST1] = [RIGHT].[TEST2]

只获得一个ROW的原因是只有一行(即第2行)的TEST1等于另一行的TEST2。

答案 2 :(得分:2)

我看起来你很快就开始Cartiesian Join。通常,如果您要返回重复项,则需要执行以下操作:

SELECT [LEFT].*
FROM [TEST]  AS [LEFT]
INNER JOIN [TEST] AS [RIGHT]
    ON [LEFT].[test1] = [RIGHT].[test1]
        AND [LEFT].[test2] = [RIGHT].[test2]
        AND [LEFT].[id] <> [RIGHT].[id]

如果您需要混合列,然后混合所需的条件,但执行以下操作:

SELECT [LEFT].*
FROM [TEST] AS [LEFT]
INNER JOIN [TEST] AS [RIGHT]
    ON (
        [LEFT].[test1] = [RIGHT].[test2]
            OR [LEFT].[test2] = [RIGHT].[test1]
       )
        AND [LEFT].[id] <> [RIGHT].[id]

使用它,您可以在每个联接中比较左侧和右侧的权利,完全不需要WHERE。

但是,这种查询样式在插入表中的每一行的执行时间呈指数级增长,因为您要将每行与每行进行比较。

答案 3 :(得分:0)

如果我没有弄错的话,这可以通过内连接来完成。 这是我第一次回答mysql的问题,但我只是回答在StackOverflow上获得更多积分。 逗号非常重要,以便mysql不会抱怨。

SELECT [LEFT].[ID] FROM [TEST] AS [LEFT], [TEST] AS [RIGHT] 
WHERE [LEFT].[ID] != [RIGHT].[ID] 
AND [LEFT].[TEST1] = [RIGHT].[TEST2];