Mysql query to display records not linked to a particular company

Asked

Viewed 130 times

0

I need to create a query on 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 build a query to display only keywords that have not been linked to the undertaking specified in the consultation.

Example:

we will consider that the key word table has 5 key words: A, B, C, D and E, and that the company "Good Idea Supermarket" has a link with the words A, C and E. Therefore, I need the query to return to me only B and D when this company is researched.

Detail: it is not only about listing records that have no match, but also filtering the records according to the company specified in the survey.

1 answer

0


You have to take everything from the word table and make a left Join with the table of links, then in the Where clause, add the condition where link is null, as you did not find any word 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
  • Hello Arllon, I did just that, it worked perfectly, the big problem is that it is not specifying a company in the query, IE, it is filtering the unlinked keywords considering all companies and not just a.

  • only add AND vin_company = id_company

  • I did, but then it doesn’t display any keyword, regardless of whether it’s linked or not. :/

  • the big problem is exactly time to pull the unlinked keyword for a particular company.

  • I edited, the filter has to be in JOIN

  • Perfect Arllon, it worked right, thank you very much!

  • Quiet, for nothing!

Show 2 more comments

Browser other questions tagged

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