user1615343
user1615343

Reputation: 43

MySQL find LIKE items in same column

I have a MySQL database containing names and addresses, but there are some duplicate names with only partial addresses such as

Name  | Address 
========================
Smith | 10 Main St., Cincinnati, OH 45202
Smith | 10 Main St.

I need a SQL query to select all rows with a duplicate name and the shorter of the two LIKE addresses. I was able to use GROUP BY and COUNT() for exact duplicates, but I don't know of a way to apply that to substrings like the example above. Any ideas? Thanks.

Upvotes: 2

Views: 130

Answers (1)

Asaph
Asaph

Reputation: 162851

Something like this should work:

select * from mytable as t1
 inner join mytable as t2
 on t1.Name = t2.Name
 and t1.Address like concat(t2.Address, '%')
 and t1.Address != t2.Address;

Upvotes: 2

Related Questions