A
A
AlexAll2021-05-22 20:36:59
SQL
AlexAll, 2021-05-22 20:36:59

How to select records from a table whose id's are not in another table?

The first table NEWS has two fields id and user_id the second table has two fields (this is a composite PrymaryKey) news_id and user_id
How to select all records from the first table, provided that the second table does not have a composite key (news_id and user_id)

Found only this

SELECT *
  FROM 'users'
 WHERE 'users'.'user_id' NOT IN(
     SELECT 'member_id' 
       FROM 'members')


But this only works if there is a check for one id, how to do this with two id?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-05-22
@AlexAll

You can use the WHERE NOT EXISTS (...) construct :

select *
from news
where not exists (
  select 1 from user_news 
  	where user_news.news_id = news.id and  user_news.user_id = news.user_id
);

SQL fiddle (MariaDB)

R
Rsa97, 2021-05-22
@Rsa97

LEFT JOIN, WHERE IS NULL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question