hayfreed
hayfreed

Reputation: 547

Excluding column from MySQL SELECT distinct query with UNION across tables

I have an SQL database with several tables of patient data. Every table has one column in common, an ID number representing each patient. There is significant overlap between the tables, i.e. the same patient ID number often appears on multiple tables. What I would like to do is SELECT all distinct patient ID numbers that do not appear on one specific table.

Upvotes: 0

Views: 338

Answers (1)

Gurwinder Singh
Gurwinder Singh

Reputation: 39467

You can use UNION and NOT IN like this:

select id
from (
    select id from table1
    union
    select id from table2
    union
    select id from table3
    ...
) t where id not in (
    select id from sometable
);

Upvotes: 1

Related Questions