Get how many times an ID appears in another MySQL table

0

I have a table "clients" that contains the data of clients and another "friends" that contains the data of friends invited by a client. I want by means of a Select to obtain the mail of the clients who have invited a multiplo of 5 friends.

I have something like that, but it does not work for me

 SELECT id_clientes, email 
 FROM clientes WHERE (SELECT COUNT(*) FROM amigos WHERE id_clientes = id_clientes) % 5 = 0;

Could someone help me? Thanks in advance

    
asked by ZyMonogatari 11.04.2017 в 01:18
source

1 answer

0

You can use the function mod in a combination with the function count , to the total count of friends of a client, you calculate the remainder of dividing it by 5. To be able to use these functions as a search criterion, you have to use the having clause .

select a.id_clientes, a.email
from clientes a, amigos b
where a.id_clientes = b.id_clientes
group by a.id_clientes
having mod(count(a.id_clientes),5) = 0
    
answered by 11.04.2017 / 05:51
source