Jump to content

[SOLVED] Grab similars?


bcoffin

Recommended Posts

Suppose my table looks like this:

 

id|firstname|lastname

 

and example rows looks like this:

 

1|adam|anderson

2|adam|allen

3|adam|allen

4|bob|barker

5|bob|barker

6|bob|brown

 

..but there's 100s of 1000s of records...

 

What's a good, fast query to select the rows that have duplicate first and last names?

 

So that only rows with IDs 2,3,4,5 are returned?

 

is there a

Link to comment
Share on other sites

you can use the COUNT() function along with GROUP BY and HAVING:

 

SELECT COUNT(id) AS count, firstname, lastname FROM table GROUP BY CONCAT(firstname, lastname) HAVING count > 1

 

this should, in theory, select the firstname and lastname (along with the count) of any firstname/lastname pair (which is why we're grouping by CONCAT()) having a count greater than 1.  you can then use these names to do whatever you'd like (to get the id's, simply select where firstname and lastname match).

 

this should work in principle - i haven't used CONCAT in a group by clause before.  to find out about these functions, id encourage you to go to the functions and operators section of the mysql manual.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.