Click to See Complete Forum and Search --> : MySQL: SELECTing rows with duplicates of a particular column


ayvegh
09-20-2008, 10:34 PM
Hi,

I have a MySQL table which has a username column.
It's a log, so many of the usernames have been logged twice.

Now, I have changed the log format, so that only one entry is necessary for each user.

My question: How can I SELECT all of the rows which have a username which is duplicated in another row?
I'd like to be able to have my script read through the data and throw out what I don't need, and merge everything else, but I need a way to get only the rows which are duplicated.

Thanks in advance,
ayvegh

MyWebsiteAdvise
09-21-2008, 02:34 PM
Hi,

There are a few ways to do it.

Here are two of them:

select t1.username
from tbl_log t1
where 1 < (select count(username)
from tbl_log t2
where t1.username = t2.username)

select username
from tbl_log
group by username
having count(*) > 1