[Common SQL] MySQL query duplicate data

To query repeated data, you can use the GROUP BY and HAVING clauses in SQL. The following is an example query that checks table_namefor duplicate column_namecolumn values ​​in the table named:

SELECT
	column_name,
	COUNT(*) 
FROM
	table_name 
GROUP BY
	column_name 
HAVING
	COUNT(*) > 1;

The query will column_namegroup by the value of the column and count the number of occurrences of each value. Then use the HAVING clause to filter groups with occurrences greater than 1, and the rows in these groups are repeated data.

Note that the above query only checks for duplicate data for one column. If you want to check for duplicate combinations of columns, include the names of those columns in the GROUP BY clause. For example:

SELECT
	column_name1,
	column_name2,
	COUNT(*) 
FROM
	table_name 
GROUP BY
	column_name1,
	column_name2 
HAVING
	COUNT(*) > 1;

The query will group by the values ​​of the column_name1and columns and count the number of occurrences of each group. column_name2Then use the HAVING clause to filter groups with occurrences greater than 1, and the rows in these groups are repeated data.

Guess you like

Origin blog.csdn.net/IUTStar/article/details/129261924