Select two tables

Asked

Viewed 113 times

2

Good afternoon! Please I’m not managing to make a select

I have two tables in the bank (campaign and campanha_clicks)

I need to select only the results with the table type=2 (CAMPAIGN) and at the same time check if in the table campanha_clicks in the id_usuario field is not the user id

I want to show only the data of those who are not yet in the id_user field of the table campanha_clicks

SELECT DISTINCT 
  c.id,
  c.site,
  c.cliques_usados,
  c.cliques,
  c.localizacao,
  c.tipo,
  c.id_user
FROM
  campanha c
INNER JOIN campanha_cliques p
WHERE p.tipo = '2' 
  AND p.id_usuario != '$uiddw'

tried that but it doesn’t work

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • And what relates the campaign table to the campanha_cliques table? Or do you just want the Cartesian product of the two tables?

  • I managed to resolve thanks for the help

3 answers

0

SELECT DISTINCT 
  c.id,
  c.site,
  c.cliques_usados,
  c.cliques,
  c.localizacao,
  c.tipo,
  c.id_user
FROM
  campanha c
INNER JOIN campanha_cliques p
ON c.id = p.id_campanha
WHERE p.tipo = '2' 
  AND p.id_usuario != '$uiddw'

I believe that only what is missing is the ON that is usually used to make the link between the tables involved in JOIN.

  • It still doesn’t work! Type I will explain better, I need to get the data from the table "CAMPAIGN" with the type=2 that does not have the id_user and id_campaign in the table CAMPANHA_CLIQUES.. I want to show only the data that has not yet in the table campanha_clicks.

  • got it, thanks

0

Your question is a little confused but maybe it is:

SELECT 
  c.id,
  c.site,
  c.cliques_usados,
  c.cliques,
  c.localizacao,
  c.tipo,
  c.id_user
FROM campanha c 
WHERE NOT EXISTS (SELECT * FROM campanha_cliques p WHERE p.tipo = '2' AND p.id_usuario == c.id_user);

or:

SELECT 
  c.id,
  c.site,
  c.cliques_usados,
  c.cliques,
  c.localizacao,
  c.tipo,
  c.id_user
FROM campanha c LEFT OUTER JOIN campanha_cliques p ON (p.id_usuario == c.id_user)
WHERE p.tipo = '2' AND p.id_usuario IS NULL;
  • got it, thanks

  • And what was it? It is good to be documented the solution so that you come to research.

0

Correct me if I’ve got it wrong:

SELECT DISTINCT 
  c.id,
  c.site,
  c.cliques_usados,
  c.cliques,
  c.localizacao,
  c.tipo,
  c.id_user
FROM
  campanha c
LEFT JOIN campanha_cliques p on p.id_campanha = p.campanha
WHERE p.tipo = '2' 
  AND c.id_user = '$uiddw'
  AND p.i_campanha is null

This select will return the table data campanha with tipo = 2, id_user = '$uiddw', where there are NO equivalent records in the campanha_cliques.

That’s it?

  • got it, thanks

  • 1

    If this or any of the answers have been helpful, upvote. If any have been the correct answer, mark as correct. If not, post the solution and then mark your answer as correct. This all helps future users who have a similar question.

Browser other questions tagged

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