简体   繁体   English

在 SQL DB2 中,如何检查一个表中的值列表是否在另一个表中可用?

[英]In SQL DB2, how would I check if a list of values in one table is available in another table?

I have two tables.我有两张桌子。 One is a comparatively small table with 20000 records with columns with id(Unique key),name, zipcode.一个是一个相对较小的表,有 20000 条记录,其列具有 id(唯一键)、名称、邮政编码。 And the other is a huge table with nearly 1 billion+ records with columns id(unique key), name, age, address & active status(boolean).另一个是一个巨大的表,包含近 10 亿条记录,列 id(唯一键)、名称、年龄、地址和活动状态(布尔值)。 I want to have the records which are not active in the second table and check if those inactive records are available in the first table.我想要在第二个表中不活动的记录,并检查这些不活动的记录是否在第一个表中可用。 I don't know how to loop the records in the first table as a single query.我不知道如何将第一个表中的记录作为单个查询循环。 How can I do that in Db2?我怎么能在 Db2 中做到这一点?

You may use EXISTS logic here:您可以在这里使用EXISTS逻辑:

SELECT t1.*
FROM Table1 t1
WHERE EXISTS (SELECT 1 FROM Table2 t2 WHERE t2.id = t1.id AND t2.status = false);

Note that the above query might benefit from the following index on the second table:请注意,上述查询可能会受益于第二个表上的以下索引:

CREATE INDEX idx2 ON Table2 (id, status);

This might let the lookup proceed much faster.这可能会让查找进行得更快。 Note that we chose to express your logic by scanning the first table, and looking up in the second, as the first table is much smaller than the second.请注意,我们选择通过扫描第一个表并在第二个表中查找来表达您的逻辑,因为第一个表比第二个小得多。

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

相关问题 如何在一个表的列中选择某些值,而另一表则不可用 - How would I select certain values in a column in one table that is not available in a another table DB2 SQL-选择与另一个表相关的表的所有值 - DB2 SQL - Select all values of a table related to another table 将 sql 表 (t1) 从一个 db (db1) 导入到另一个 (db2) (t2),然后使用 t2 更新 db2 中表的值 - Importing sql table (t1) from one db (db1)to another (db2) (t2)and then use t2 to update values of a table in db2 DB2 SQL-如何在另一个表的结果字段上联接表 - DB2 Sql - How to join a table on a resulting field of another table 如何从 DB2 中的另一个表中存在行的表中删除? - How can I delete from one table where rows exist in another table in DB2? 如何在DB2中将LIKE运算符与另一个表中的值一起使用? - How to use LIKE operator in DB2 with values from another table? 如何检查表示多个每小时时间序列 (DB2) 的表中的缺失值? - How do I check missing values from a table which represents multiple hourly time series (DB2)? 在SQL Server中DB2中的另一个表更新之后,如何更新DB1中的表? - How to update a table in DB1 after another table in DB2 update in sql server? 记录存在于一个 DB2 表中,但不存在于另一个表中 - Record exist in one DB2 table but not in another table SQL DB2 / 400:检查是否存在全局临时表 - SQL DB2/400 : check existence global temporary table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM