如何检查是否存在id列表

时间:2016-12-27 15:45:47

标签: sql postgresql

我希望做一个单独的查询,如果存在(true或false)一个id列表而不是一个,则返回。

常见的存在查询就像下一个

select exist(select 1 from one_table where id_table = 'some_value') 

我想要一个检查多个值的查询。这将是下一个

select exist(select 1 from one_table 
             where id_table in('some_value_1', '...')) 

但是,在我寻找所有Ids的情况下,必须存在。

1 个答案:

答案 0 :(得分:2)

一种方法使用count(*)

select <n> = (select count(*) from one_table where id_table in ('some_value_1', '...')

<n>将是ID的数量。

另一种方法是使用exists

的序列
select (exists (select 1 from one_table where id_table = id1) and
        exists (select 1 from one_table where id_table = id2) and
        . . .
       )
相关问题