Perform a query by checking if this record already exists in another table

Asked

Viewed 33 times

1

The logic is as follows, I have a table "TBL_NOTICIAS" and another table with name "TBL_DESBLOQUEADO".

When the user accesses the page I present all the news and he can unlock the news you want to read, and when he unlocks I make a record in the table "TBL_DESBLOQUEADO" with the client id and news id so that when he access the page again, the news that he has already unlocked does not appear.

My query would need to be like this: SELECT * FROM tbl_noticias WHERE ... (when not available in tbl_unlocked)

Table logic:

TBL_NOTICIAS

  • id
  • title
  • news

TBL_DESBLOQUEDO

  • id
  • id_client
  • id_noticia

1 answer

0


You can use the NOT EXISTS to the subquery, thus:

SELECT * 
  FROM tbl_noticias 
 WHERE NOT EXISTS (SELECT * FROM tbl_desbloqueado
                    WHERE id_cliente = 123 -- id do cliente
                      AND id_noticia = tbl_noticias.id)
  • It worked perfectly! Thank you very much!

Browser other questions tagged

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