表名列出所需的sql查询帮助

表名列出所需的sql查询帮助,sql,Sql,我有一个表,表1包含从1到10的条目,还有20个其他表在一列中使用值1到10。 我想列出所有表名,这些表名包含表1中缺少相应项的条目 例如: 因此,查询的输出应该为我提供表3和表5 SELECT DISTINCT TableName FROM (SELECT 'Table_2' as TableName, col FROM Table_2 UNION ALL SELECT 'Table_3' as TableName, col FROM Table

我有一个表,表1包含从1到10的条目,还有20个其他表在一列中使用值1到10。 我想列出所有表名,这些表名包含表1中缺少相应项的条目

例如:

因此,查询的输出应该为我提供表3和表5

SELECT DISTINCT TableName
FROM (SELECT 'Table_2' as TableName, col
      FROM Table_2
      UNION ALL
      SELECT 'Table_3' as TableName, col
      FROM Table_3
      /*repeat for all tables*/
      UNION ALL
      SELECT 'Table_20' as TableName, col
      FROM Table_20) x
      LEFT JOIN Table_1 t ON x.col = t.col
WHERE t.col is NULL

这应该行得通。如果使用的是sql server/Oracle,还可以使用减号和/或except集合运算符。不确定您使用的是什么数据库

select 'in tbl 2 but not tbl 1' as which, numcol from table_2 where numcol not in (select numcol from table_1)
union all
select 'in tbl 3 but not tbl 1' as which, numcol from table_3 where numcol not in (select numcol from table_1)
union all
select 'in tbl 4 but not tbl 1' as which, numcol from table_4 where numcol not in (select numcol from table_1)
union all
select 'in tbl 5 but not tbl 1' as which, numcol from table_5 where numcol not in (select numcol from table_1)

了解这些,你就能回答你自己的问题。我已经阅读了所有关于连接的内容,但不知道如何在查询输出中列出表名。对集合论的基本理解也很有帮助。在VeN图中,每一个表都是一个圆圈,你在寻找表2/3/4/5的部分,它不重叠表1。你使用的是什么数据库?它是一个项目需求,我提供了一个示例场景。假设我有一个源文件表,其中包含所有源文件ID的条目。还有其他表格,比如合同,取消合同等等,对于这些表中的每条记录都有一个对应的源编号。因此,当我对源编号进行区分时,我会得到表中存在的不同值。现在我想列出表名,如果它在源编号列中有任何无效的条目。此查询工作正常,谢谢。但是有没有一种方法可以让我不必硬编码所有的表名,而是使用#表列出sys.all_对象中的所有表,并一次检索一行?感谢您不用动态SQL
select 'in tbl 2 but not tbl 1' as which, numcol from table_2 where numcol not in (select numcol from table_1)
union all
select 'in tbl 3 but not tbl 1' as which, numcol from table_3 where numcol not in (select numcol from table_1)
union all
select 'in tbl 4 but not tbl 1' as which, numcol from table_4 where numcol not in (select numcol from table_1)
union all
select 'in tbl 5 but not tbl 1' as which, numcol from table_5 where numcol not in (select numcol from table_1)