I used the following query to find duplicates:
SELECT userID, COUNT(userID) AS NumOccurrences FROM userDepartments GROUP BY userID HAVING ( COUNT(userID) > 1 )
I then tried adding an inner join so I could see the user names that match, which are stored in a different table.
SELECT userDepartments.userID, users.firstname, users.lastname, COUNT(userID) AS NumOccurrences FROM userDepartments INNER JOIN users ON userDepartments.userID = users.userID GROUP BY userID HAVING ( COUNT(userID) > 1 )
But it gave me an error saying that users.firstname was not part of some aggregate function or something…
Does anyone know how I can get the count, only show users with more than 1 department, and also get the first and last name out of the other table so I can get a list of users names who have more than one department assigned?
EDIT: THIS IS THE QUERY THAT ENDED UP WORKING FOR ME…
SELECT firstname, lastname FROM tbl_users WHERE (userID IN (SELECT userID FROM tbl_usersDepts GROUP BY userID HAVING (COUNT(userID) > 1)))
I would rearrange the query a little bit….