我有一个查询没有返回结果:
SELECT review_id
FROM review_table
WHERE review_id NOT IN (
SELECT DISTINCT review_id
FROM review_migration_table
)
ORDER BY review_id
然而,我希望它返回不在我的迁移表中的所有review_id
(我正在努力识别所有未成功迁移的评论)。我期望非空结果的原因是我知道review_table
中有不在review_migration_table
:中的评论
SELECT
review_id,
review_id NOT IN (
SELECT DISTINCT review_id
FROM review_migration_table
WHERE filter_column = 170938)
FROM review_table
WHERE
filter_column = 170938
ORDER BY review_id ASC
review_id | ?column?
-----------+----------
127260864 | f
130811274 |
(2 rows)
为什么Redshift为该查询中的第二条记录返回"NULL"值?更具体地说,我如何让我的第一个查询返回130811274(和其他评论),这些评论在review_table
中,但不在review_migration_table
中?
您可以认为a NOT IN(b, c)
等同于NOT (a=b OR a=c)
。我认为review_migration_table表的review_id
列中有一个NULL值,因此您有NOT (a=b OR a=NULL)
,如果a=b
,它将给出FALSE
;如果a<>b
,它将为NULL。这里最简单的解决方法可能是从IN
列表中筛选出NULLreview_id
,例如:
SELECT
review_id,
review_id NOT IN (
SELECT DISTINCT review_id
FROM review_migration_table
WHERE filter_column = 170938 and review_id IS NOT NULL)
FROM review_table
WHERE
filter_column = 170938
ORDER BY review_id ASC