With LEFT JOIN list the NOT IN

2

In Mysql I have the table usuario , with the fields ID and CPF :

IreceivedalistofsomeCPFsfromtheclientandwhatIneed:

SeewhichoftheseusersisNOTintheusuario_gerenciamentotable,theproblemisthattheusuario_gerenciamentotabledoesnothavetheusers'CPFs,buttheirIDs,likethis:

That is, this query I need to do should list the 2 and 6 IDs, how do I? I tried with LEFT JOIN and NOT IN , but it does not return right.

    
asked by anonymous 06.12.2018 / 13:48

3 answers

5

Do with LeftJoin to fetch all of the left table's table record and with that the ones that do not have side can be brought with IS NULL which are the data that has no relation to the table usuario , example :

SELECT usuario.id, usuario.cpf FROM usuario 
    LEFT JOIN usuario_gerenciamento 
       ON usuario_gerenciamento.usuario_id=usuario.id 
WHERE usuario_gerenciamento.usuario_id IS NULL
    
06.12.2018 / 14:08
4

You can use the following:

SELECT ID
FROM usu
LEFT JOIN usu_gerenciamento on ID = USUARIO_ID
WHERE USUARIO_ID IS NULL;

left join filtering what has no occurrence in usu_gerenciamento .

    
06.12.2018 / 14:07
1

I got this way:

SELECT usuario.id AS id_buscado FROM usuario
LEFT JOIN usuario_gerenciamento ON
usuario.id = usuario_gerenciamento.usuario_id
WHERE usuario_gerenciamento.usuario_id IS NULL AND usuario.cpf = '$cpf'
    
06.12.2018 / 14:04