Mailing List
Home
Forum Home
MySQL General - General MySQL discussion
MySQL++ - Programming with the C++ API to MySQL
MaxDB - Everything about MaxDB, formerly known as SAP DB
ODBC - ODBC with the MySQL Connector/ODBC driver
MySQL on Win32 - Runing MySQL on Windows 9x/Me/NT/2000/XP
Java Help - Mostly related to the MySQL Connector/J driver
Perl - Perl support for MySQL with DBI and DBD::mysql
GUI - MySQL GUI Tools
Announcement
Subjects
Subject: mysql openssl Question
ERROR 1045: Access denied for user: 'root@localhost ' (Using
password: NO)
Update one field with more fields from another table
Subject: Getting Identity after INSERT
ERROR 2002: Can 't connect to local MySQL server through socket
mysql test 4 1 fails with the gis test
Subject: MySQL Cluster Software
Downgrade Mysql from 4 to 3 23
Mysql 4 0 Oracle Stored Procedure Trigger Conversion
Can 't access mysql after kernel upgrade
Executing MySQL Commands From Within C Program
Comparing and writing out BLOBS
Subject: Re: Preventing Duplicate Entries
FULLTEXT query format question
Strange behavior, Table Level Permission
Does the binary log enabling affect the MySQL performances?
mysql:it 's a db not a dbms how it 's possible?!
mysql have same function mthod as Oracle decode()
 
Subject: Join to find Duplicates

Subject: Join to find Duplicates

2007-10-16       - By Chris W

 Back
I need to find records in a table that may be duplicate records.

The table stores basic information about the Users.  I want to do a
match on the FName and LName fields.  The query I have looks like this....

SELECT u1.UserID, u1.FName, u1.LName, u1.Email, COUNT(u1.Email) AS `Count`
FROM user u1
JOIN user u2 ON u1.FName = u2.FName AND u1.LName = u2.LName
GROUP BY u1.UserID
HAVING Count > 1
ORDER BY u1.LName, u1.FName

This works fine. However, I would like to help determine which of the
duplicates should be removed buy getting data from another table.  That
is were I get lost.

The second table has "User Profile" records for each user.  It is a one
to many relationship so each user can have 0 or more profile records.  
What I would like to add to this query is a count of how many profile
records each UserID has in the User profile table.  That way if there
are two users with the same name and one has 5 profile records and the
second has no profile records it is clear which to remove.  What I have
come up with is....

SELECT u1.UserID, u1.FName, u1.LName, u1.Email, COUNT(u1.Email) AS
`Count`, COUNT(p.UserID) as ProfileCount
FROM user u1
JOIN user u2 ON u1.FName = u2.FName AND u1.LName = u2.LName
LEFT OUTER JOIN userprofile p ON u1.UserID = p.UserID
GROUP BY u1.UserID
HAVING `Count` > 1
ORDER BY u1.LName, u1.FName

However that returns rows that are not duplicate names and I'm not sure why.

--
Chris W
KE5GIX

"Protect your digital freedom and privacy, eliminate DRM,
learn more at http://www.defectivebydesign.org/what_is_drm"



--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/mysql?unsub=mysql@(protected)