寻找互斥的对?

时间:2018-03-21 13:22:53

标签: sqlite

有一张桌子"喜欢"字段" ID1"和" ID2"哪里有互斥的对,我想找到它们 我通过连接尝试它并且它没有工作,我无法弄清楚原因 enter image description here

3 个答案:

答案 0 :(得分:1)

通常,您会将GROUP BY [column]HAVING COUNT([column|*]) >= [number]结合使用以查找列中的重复项

SELECT 
 name
FROM 
 Highschooler 
GROUP BY
 name
HAVING 
 COUNT(*) >= 2
ORDER BY
 name ASC

如果您需要有关高中学生的更多信息,您需要再次加入。

SELECT 
 Highschooler.*
FROM (

    SELECT 
     name
    FROM 
     Highschooler 
    GROUP BY
     name
    HAVING 
     COUNT(*) >= 2
    ORDER BY
     name ASC
)
 AS name_duplicated
INNER JOIN 
 Highschooler 
ON
 name_duplicated.name = Highschooler.name

答案 1 :(得分:0)

如果我已正确解释您的问题,您可以这样做:

SELECT name
FROM highschooler
GROUP BY name
HAVING count(*) > 1

它会在highschooler

中返回所有出现不止一次的名字

答案 2 :(得分:0)

如果您尝试以任何顺序查找重复对,则可能有很多方法可以执行此操作。这是一个:

create table likes(id1,id2);
insert into likes values
(1689,1709),
(1709,1689),
(1782,1709),
(1911,1247),
(1247,1468),
(1641,1468),
(1316,1304),
(1501,1934),
(1934,1501),
(1025,1101);

select min(id1,id2)||'-'||max(id1,id2) as pairs
  from likes
  group by pairs
  having count(*) > 1;