如果有一长串值,这些值恰好是postgres数据库中记录属性的值。
我想创建一个查询,找出在数据库中找不到哪些值。
我没有权利执行DDL语句,我想避免使用程序代码。
示例:
表可能是
CREATE TABLE Test (
ID Integer,
attr varchar(30)
)
列表可能类似于(但更长,大约240000个值)
ATTR
TestValue0
TestValue1
TestValue2
TestValue3
使用sed我可以创建并执行一个语句
select count(*) from Test where attr in ('TestValue0',
'TestValue1','TestValue2','TestValue3')
该声明告诉我,并非所有这些值都可以在Test中找到。
如何制定一个查询,告诉我在postgres数据库中找不到哪些uniq值?
答案 0 :(得分:0)
对于您要执行的操作,您可以使用left join
,not in
或not exists
。但关键是你需要一个包含你关心的值的派生表:
select v.attr
from (values ('TestValue0'), ('TestValue1'), ('TestValue2'), ('TestValue3')
) v attr
where not exists (select 1 from test t where t.attr = v.attr);