【问题标题】:Excluding column from MySQL SELECT distinct query with UNION across tables使用 UNION 跨表从 MySQL SELECT 不同查询中排除列
【发布时间】:2017-05-26 14:01:32
【问题描述】:

我有一个包含多个患者数据表的 SQL 数据库。每个表都有一个共同的列,一个代表每个患者的 ID 号。表格之间有很大的重叠,即相同的患者 ID 号经常出现在多个表格上。我想做的是选择所有不同的患者 ID 号码,这些号码没有出现在一个特定的表格上。

【问题讨论】:

标签: mysql


【解决方案1】:

您可以像这样使用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
);

【讨论】:

  • 谢谢!效果很好。当您说“t where id not in...”时,“t”的确切含义是什么
  • t 是子查询的别名。在子查询之外,您可以像引用真实表一样引用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-06
  • 1970-01-01
相关资源
最近更新 更多