I am trying to find duplicates by comparing the first name and surname columns in a table. The first name can be a name or an initial.
Reading other posts I have managed to figure out how to get the duplicate surnames and list the first letter for first name. But I am unsure how to only show rows where there is a match of surname and the first letter of the first name.
SELECT *
FROM table AS a
INNER JOIN (
SELECT LEFT( firstname, 1 ) , surname
FROM table
GROUP BY surname
HAVING COUNT( * ) > 1
) AS b ON a.surname = b.surname
id | firstname | surname
**************************
1 | joe | bloggs
2 | j | bloggs
3 | s | bloggs
4 | f | doe
5 | frank | spencer
Currently this query would return
1 | joe | bloggs
2 | j | bloggs
3 | s | bloggs
Result I would like would just contain the possible duplicates.
1 | joe | bloggs
2 | j | bloggs
I don’t quite get what you want. Yor provided a query, your current table and the expected result.
I’ve just created your table, run your query and got the expected result. What is wrong with this?
This effectively result in your expected result:
Or am I missing something?
After re-reading… are you expecting to get only this?
If that is the case, use this:
Edit:
After the expected result was properly explained I conclude the query should be:
Working example