简体   繁体   English

使用跨表的UNION从MySQL SELECT不同查询中排除列

[英]Excluding column from MySQL SELECT distinct query with UNION across tables

I have an SQL database with several tables of patient data. 我有一个包含多个患者数据表的SQL数据库。 Every table has one column in common, an ID number representing each patient. 每个表都有一个共同的列,一个ID号代表每个患者。 There is significant overlap between the tables, ie the same patient ID number often appears on multiple tables. 这些表之间存在很大的重叠,即,同一患者ID号经常出现在多个表上。 What I would like to do is SELECT all distinct patient ID numbers that do not appear on one specific table. 我想做的是选择所有未显示在一张特定表上的不同患者ID号。

You can use UNION and NOT IN like this: 您可以这样使用UNIONNOT IN

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
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM