With LEFT JOIN list the NOT IN

Asked

Viewed 98 times

2

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

inserir a descrição da imagem aqui

I received from the client a list with some numbers and what I need:

See which of these users is NOT in the table usuario_gerenciamento, the problem is that the table usuario_gerenciamento I don’t have users' Cpfs, but their Ids, so:

inserir a descrição da imagem aqui

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

3 answers

5


Do with Leftjoin to search all record of the table on the left and with this the ones that do not have on the right side can be brought with IS NULL which is 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

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 does not occur in usu_gerenciamento.

1

I got it that 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'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.