Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find duplicates for combination of two columns in a MySQL database

I work on a dataset with three different columns: pile, position and info.

There is no duplicate in the database, but it can happen, that for one combination of pile and position there is one or two different texts in the info column. And those are the entries I tried to find.

I tried the following

SELECT COUNT(DISTINCT(`pile`, `position`)) FROM db;

But received an error message

ERROR 1241 (21000): Operand should contain 1 column(s)

Is there a way to find distinct combinations of values in two columns?

like image 963
R_User Avatar asked Apr 24 '13 10:04

R_User


1 Answers

This works even without subselects.

SELECT
  `pile`,
  `position`,
  COUNT(*) AS c
FROM
  db
GROUP BY
  `pile`,
  `position`
HAVING c > 1;

The command above shows all combinations of pile and position that occur more than once in the table db.

like image 70
John Garreth Avatar answered Oct 12 '22 21:10

John Garreth