用于选择数据并在所选记录中查找重复值的SQL查询

时间:2015-08-21 06:48:23

标签: php mysql sql sql-server

我正在尝试创建一个将结合以下两个查询的查询。

第一次查询:

select * from mutual_friends where fb_id =8807531 OR fb_id=102061

第二次质疑:

SELECT name,picture, COUNT(picture) 
FROM mutual_friends
GROUP BY picture
HAVING ( COUNT(picture) > 1 )

第一个查询选择两个id的数据然后第二个查询在图片列中查找重复

我希望使用单个查询结果

如果有人能帮助我解决这个问题,我将非常感激

3 个答案:

答案 0 :(得分:2)

SELECT name,picture, COUNT(picture) 
FROM mutual_friends
where fb_id = 8807531 OR fb_id = 102061
GROUP BY picture
HAVING ( COUNT(picture) > 1 )

答案 1 :(得分:0)

您可以这样做:

 SELECT name,picture, COUNT(picture) 
 FROM mutual_friends
 WHERE fb_id = 8807531 OR fb_id = 102061
 GROUP BY picture
 HAVING ( COUNT(picture) > 1 )

答案 2 :(得分:0)

如果您正在寻找所有带有重复图片的ID ..那么我建议您的答案为

SELECT fb_id, name, picture 
FROM mutual_friends
WHERE picture IN (
    SELECT picture
    FROM mutual_friends 
    GROUP BY picture
    HAVING COUNT(picture) > 1)

或者更具体,因为你的问题没有清楚地解释你在寻找什么。