I have a table of baseball players(all 1000 or so), with fields:
mysql> describe person;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| firstname | varchar(30) | NO | | NULL | |
| lastname | varchar(30) | NO | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
But I think there are some players that have gotten added in twice. How can I go through and check for how many occurrences of a particular firstname, lastname combo?
For a list sorted by decreasing value of the number of copies:
The
HAVING
clause is the key part - it's necessary to filter the results after theGROUP BY
clause, since aWHERE
clause filters out rows before they're grouped.This provides the list of duplicates:
If you want to see the counts for every row remove the having clause:
To find duplicate records (ex: to find login name and password combination of duplicate records) in a table use the below query;
To get id's of duplicate names as well as names do:
If you simply want to erase all the duplicate, you could do a temporary table and fill it up with all you
re data except the duplicate and them re-update you
re primary table.The query to select the data with duplicate would be this
To get the complete list of data in you're table
With this last query you'll get a the list of data sorted by lastname Alphabeticly.