简体   繁体   English

对于mysql中的特定字段,返回计数> 1的表中的所有记录

[英]Return all records in a table with count > 1 for specific field in mysql

I know how to count records in a table based on a distinct value in a field but I am trying now to return all the records in the table that have duplicate values in a given field (without seeing the records that do not) so I can do some analysis of those records. 我知道如何根据字段中的不同值对表中的记录进行计数,但是我现在尝试返回表中所有在给定字段中具有重复值的记录(而看不到没有记录的记录),因此我可以对这些记录进行一些分析。 Can I use count this way? 我可以用这种方式计数吗? I tried 我试过了

where count (distinct FIELD_A)>1

but it said I was using group incorrectly. 但是它说我使用的组不正确。

To restrict / filter the query output after aggregation has been performed, use a HAVING clause. 要在执行聚合后限制/过滤查询输出,请使用HAVING子句。

The WHERE clause is used to restrict / filter the query output BEFORE aggregation is performed (so if you had some rows you did not want to include in the COUNT, you can use the WHERE clause to filter them out). WHERE子句用于在执行聚合之前限制/过滤查询输出(因此,如果您不想在COUNT中包含某些行,则可以使用WHERE子句将其过滤掉)。

Also you should not be using DISTINCT here, it will REMOVE all the duplicates before calculating the COUNT. 另外,您不应该在这里使用DISTINCT ,它会在计算COUNT之前删除所有重复项。

Try this: 尝试这个:

SELECT
    FIELD_A, count(*) no_of_records
FROM table
GROUP BY FIELD_A
HAVING count(*) > 1;

Use having statement 使用having声明

Select Field_Blah, Count(Distinct Field_A)
From table_a
group by Field_Blah
having Count(Distinct Field_A) > 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM