Anas Zilali Anas Zilali - 3 months ago 21
MySQL Question

Counting rows without duplicates MYSQL

I have a table such as the following

id_profile, id_user
1 , 5
1 , 4
1 , 4
1 , 4
2 , 1
2 , 5
2 , 2


I'm trying to count id_user but I have to eliminate duplicates. So, I want a result like

id_profile, count
1 , 2
2 , 3


Any suggestions?

Answer

Do a GROUP BY, use count distinct:

select id_profile, count(distinct id_user) as count
from tablename
group by id_profile
Comments