Query MySql to display records not linked to a certain company [duplicate]

0

I need to create a query in SQL .

I have three tables:

TABELA 1: EMPRESA
emp_codigo (chave primaria)
emp_nome

TABELA 2: PALAVRAS_CHAVE
pal_codigo (chave primaria)
pal_nome

TABELA 3: VINCULO
vin_codigo (chave primaria)
vin_empresa (chave secundaria de emp_codigo)
vin_palavra (chave secundaria de pal_codigo)

I'm trying to mount a query to display only the keywords that were not linked to the company specified in the query .

Example :

  

Let's consider that the table of keywords has 5 key words: A, B, C, D and E, and that the company "Supermercado Boa Ideia" has a link with the words A, C and E. Logo , I need the query to return to me only B and D when this company is searched.

Detail: This is not just about listing records that do not match, but also filtering the records according to the company specified in the search.

    
asked by anonymous 11.12.2017 / 20:41

1 answer

0

You have to get all of the word table and make a left join with the bindings table, then in the where clause, add the condition where binding is null, as it did not find any words there:

SELECT
    *
FROM palavra_chave p
LEFT JOIN vinculo v
ON v.vin_palavra = p.pal_codigo
AND v.vin_empresa = [sua_empresa]
WHERE v.vin_codigo IS NULL
    
11.12.2017 / 21:13