Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 重复字段值选择_Mysql_Sql - Fatal编程技术网

Mysql 重复字段值选择

Mysql 重复字段值选择,mysql,sql,Mysql,Sql,桌上有宠物 id |字段a |字段b 需要通过巧合来选择一个字段,这不仅仅是巧合。 比如说 id | field_a | field_b 1 | cats | 13 2 | cats | 15 3 | cats | 16 4 | dogs | 15 5 | dogs | 16 6 | birds | 13 7 | birds | 19 8 | birds | 14 id |字段a |字段b 1 |猫| 13 2只猫15只 3只猫16只

桌上有宠物


id |字段a |字段b

需要通过巧合来选择一个字段,这不仅仅是巧合。 比如说

id | field_a | field_b 1 | cats | 13 2 | cats | 15 3 | cats | 16 4 | dogs | 15 5 | dogs | 16 6 | birds | 13 7 | birds | 19 8 | birds | 14 id |字段a |字段b 1 |猫| 13 2只猫15只 3只猫16只 4只狗15只 5只狗16只 6 |鸟| 13 7 |鸟| 19 8 |鸟| 14 因此,我们获得了

cats dogs 猫 狗
之所以获得此结果,是因为只有这些记录匹配字段_b(15和16)中的多个条目。

您可以尝试以下查询:

SELECT p1.field_a 
FROM pets p1 
INNER JOIN pets p2 ON (p1.field_b = p2.field_b AND p1.id != p2.id) 
GROUP BY p1.field_a,p2.field_a 
HAVING count(p1.field_b) > 1

问题是什么?“巧合选择”——从未听说过。请检查一下,您是否希望返回不同的
字段a
,对于该字段b,它不是唯一的,对吗?@ninesided,是的。在示例中,猫和狗在字段_b(15和16)中具有相等的值,并且此记录的计数更多1@AlexCoder但是猫和鸟都有值为13的行,不包括在内,这是为什么?不,因为我们不知道字段_-bOk中的值,我希望现在我得到你想要的:)我已经编辑了我的答案。现在,您将只得到与其他宠物至少在2个字段_b值上匹配的宠物。