Select records that exist less than 3 times in a table

2

Hello, I have the following select:

select u.idusuario, u.apellido, u.nombre, u.login, u.email 
from USUARIO u left join CREDENCIAL  
c on u.idusuario=c.idusuario
where c.idusuario is NULL

Which is useful to bring the records that do not exist in the table Credential but that do exist in the table User , I would like to know how I can do to show once all the users that are found to be repeated 2, 1 or none at the Credential table.

    
asked by Agustin Coronel 03.05.2018 в 23:08
source

2 answers

1

Try this:

select c.idusuario, count(*) as Cantidad
from CREDENCIAL c 
group by c.idusuario
having count(*) >= 3

If you need to know who those users are, you can make a join with a subquery or a cte and use the same logic above

    
answered by 04.05.2018 в 04:05
-1

If I understood correctly, this is what you want

select
    u.idusuario, u.apellido, u.nombre,Count(c.idusuario) as existe
from USUARIO u 
    left join CREDENCIAL c on u.idusuario=c.idusuario
group by c.idusuario
    
answered by 03.05.2018 в 23:27