Trying To Output Duplicate Entries in table based on another table entry

I have a database with fields like the following:

id -- user id -- role id

1  --- 100 ----- 119
2  --- 100 ----- 119
3  --- 101 ----- 101
4  --- 102 ----- 250
5  --- 103 ----- 300

Open in new window


I'd like to be able to feed in a list of user ids, and have it return which role id's are duplicated. For example, for the sample above, it would print out something like the following:

100 --- 119 --- 2

Open in new window


I'd like a query to be able to delete these duplicates as well, after I run the select to see the results

I've toyed with the following:

select user_id, role_id, count(*)
from user_roles
where user_id in (333,272,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,251,349,350,351,353,354,355,356,358,359,319,257,360,317,255,318,361,332,362,363,364,253,365,366,367,368,369,269,370,256,371,372,374,373,375,376,262,252,378,379,380,254,381,382,383,384,385,386,387,388,389,390)
group by role_id having count(*) > 1

Open in new window


but it doesn't present accurate results
user-roles.csv
TampaJayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Your query is 99% there - you left out the user_id from the group by clause.

select user_id, role_id, count(*)
from user_roles
where user_id in (333,272,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,251,349,350,351,353,354,355,356,358,359,319,257,360,317,255,318,361,332,362,363,364,253,365,366,367,368,369,269,370,256,371,372,374,373,375,376,262,252,378,379,380,254,381,382,383,384,385,386,387,388,389,390)
group by user_id, role_id having count(*) > 1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TampaJayAuthor Commented:
Thanks for quick response!

Any idea for delete syntax? (to delete the multiples and just leave one unique role_id per user_id)
0
TampaJayAuthor Commented:
NVM, found it :-)

delete b
from user_roles a
join user_roles b on a.id < b.id
  and a.user_id = b.user_id
  and a.role_id = b.role_id
where a.user_id in (select id from users where user_id in ('A789026','A808844'));

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.