Jump to content


Photo

Grouping Items in MySQL


  • Please log in to reply
2 replies to this topic

#1 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 03 May 2006 - 06:24 PM

I have the following:

SELECT *, COUNT(*) FROM users GROUP BY COUNT(*)

Now, how can I show the results from above where COUNT(*) is greater than 1? What I am trying to do if find all the duplicate usernames (i'm trying to clean up an old database before I use it in a new project)

If I make the username field unique, it only shows me the duplicates one at a time. Quite tedious.





#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 04 May 2006 - 03:45 AM

Sounds like you want following:

SELECT username, COUNT(*) AS dups FROM users GROUP BY username HAVING dups > 1

Hope that helps.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 04 May 2006 - 02:31 PM

[!--quoteo(post=371140:date=May 3 2006, 08:45 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ May 3 2006, 08:45 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Sounds like you want following:

SELECT username, COUNT(*) AS dups FROM users GROUP BY username HAVING dups > 1

Hope that helps.
[/quote]


Ahh thats it.

Thanks!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users