Answer the question
In order to leave comments, you need to log in
How to select elements from a table that are not contained in the second one?
There is a table x, with id and title fields, there is a table y with id, x_id fields.
Accordingly, it is necessary to select all id's from table x that are not in table y.
Decision
select id from x where id not in (select x_id from y);
Answer the question
In order to leave comments, you need to log in
To increase overall performance, you need to force RDBMS to use merge, and not hash join and not nested loop using an index.
To do this, we merge two tables
SELECT id FROM x
UNION ALL
SELECT id FROM x
UNION ALL
SELECT x_id FROM y
SELECT id
FROM (SELECT id FROM x
UNION ALL
SELECT id FROM x
UNION ALL
SELECT x_id FROM y
)
GROUP BY id
HAVING COUNT(*) = 2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question