MySQL/MariaDB queries duplicate data in a certain/multiple fields

Create test tables and data

# 创建表
create table if not exists t_duplicate (
  name varchar(255) not null,
  age int not null
);

# 插入测试数据
insert into t_duplicate(name, age) values('a', 1);
insert into t_duplicate(name, age) values('a', 2);

Query for duplicates in a single field

Use the count() function, group by grouping and having grouping to filter

select name, count(*) count
from t_duplicate
group by name
having count > 1;
  • group by name: nameGroup according to field.
  • count(*): Calculate the number of records in each group.
  • having count > 1: After grouping, filter the groups whose record number is > 1.

search result:

name count
a 2

Using subqueries and the in function

select *
from t_duplicate
where name in (
  select name
  from t_duplicate
  group by name
  having count(*) > 1
)
  • Subquery: According to namegrouping, filter the grouping whose record number is > 1, that is, the query is repeated name.
  • External query: Use to infilter nameduplicate records.

search result:

name age
a 1
a 2

Use window functions over and partition by partition

select `name`, count
from (
  select name, (count(*) over (partition by name)) as count
  from t_duplicate
) t
where count > 1
  • partition by name: namePartitioned by fields, the same namevalues ​​are in one partition.
  • count(*) over: Count the number of records for each partition.
  • count > 1: Filter data with partition record number > 1.

search result:

name count
a 2
a 2

Query multiple fields for duplicates

……

Guess you like

Origin blog.csdn.net/duanluan/article/details/132730114