Postgres:如何在DB中查找缺席用户

时间:2017-11-20 22:17:30

标签: postgresql

需要比较两个db时的常规任务。 Somene向我发送了用户列表,该列表应该在我的数据库中。 127个职位。

当我SELECT * FROM users WHERE id IN (...list of sent ids)时,我只找到100个用户。

如何选择列表中但不在我的数据库中的人?

我尝试过这样的事情:

    SELECT id FROM
  (SELECT '089477','089485','089417','089419','089416','089415','082513','087201','084769','083467','089498','089394','085097','084818','089497','085208','082924','087204','087257','084708','0844187','089119','088475','089448','084824','089436','085200','086431','089444','089479','089486','089460','089442','089449','089413','089420','084917','084702','089433','089437','089443','081804','088813','089480','089441','087184','081806','089435','081784','089401','089434','089423','089384','089422','089382','089476','089473','089406','089461','089404','089409','089410','089412','089411','089396','089006','089381','089379','089378','089397','089405','080006','089293','089478','084846','085210','089453','089400','089452','089389','089383','089456','089402','089394','089418','089392','089387','089399','089101','089117','080163','086021','081059','089414','089108','089288','089447','089446','089388','089445','089386','089430','088828','088375','089407','083429','088645','089377','089342','089337','089332','081635','089426','087197','089425','087767','088395','089341','089349','082114','082123','084687','089333','089297','087371','089331') as all_users

WHERE id NOT IN(
SELECT trader_systems_id as id FROM users WHERE
  trader_systems_id IN (
    '089477','089485','089417','089419','089416','089415','082513','087201','084769','083467','089498','089394','085097','084818','089497','085208','082924','087204','087257','084708','0844187','089119','088475','089448','084824','089436','085200','086431','089444','089479','089486','089460','089442','089449','089413','089420','084917','084702','089433','089437','089443','081804','088813','089480','089441','087184','081806','089435','081784','089401','089434','089423','089384','089422','089382','089476','089473','089406','089461','089404','089409','089410','089412','089411','089396','089006','089381','089379','089378','089397','089405','080006','089293','089478','084846','085210','089453','089400','089452','089389','089383','089456','089402','089394','089418','089392','089387','089399','089101','089117','080163','086021','081059','089414','089108','089288','089447','089446','089388','089445','089386','089430','088828','088375','089407','083429','088645','089377','089342','089337','089332','081635','089426','087197','089425','087767','088395','089341','089349','082114','082123','084687','089333','089297','087371','089331'
  )
)

但没有运气猜测。

2 个答案:

答案 0 :(得分:1)

您可以将外部联接用于值列表:

SELECT t.id 
FROM (
  values 
     ('089477'),('089485'),('089417'),('089419'),('089416'),('089415'),('082513'),('087201'),('084769'),('083467'),('089498'),
     ('089394'),('085097'),('084818'),('089497'),('085208'),('082924'),('087204'),('087257'),('084708'),('0844187'),('089119'),
     ('088475'),('089448'),('084824'),('089436'),('085200'),('086431'),('089444'),('089479'),('089486'),('089460'),('089442'),
     ('089449'),('089413'),('089420'),('084917'),('084702'),('089433'),('089437'),('089443'),('081804'),('088813'),('089480'),
     ('089441'),('087184'),('081806'),('089435'),('081784'),('089401'),('089434'),('089423'),('089384'),('089422'),('089382'),
     ('089476'),('089473'),('089406'),('089461'),('089404'),('089409'),('089410'),('089412'),('089411'),('089396'),('089006'),
     ('089381'),('089379'),('089378'),('089397'),('089405'),('080006'),('089293'),('089478'),('084846'),('085210'),('089453'),
     ('089400'),('089452'),('089389'),('089383'),('089456'),('089402'),('089394'),('089418'),('089392'),('089387'),('089399'),
     ('089101'),('089117'),('080163'),('086021'),('081059'),('089414'),('089108'),('089288'),('089447'),('089446'),('089388'),
     ('089445'),('089386'),('089430'),('088828'),('088375'),('089407'),('083429'),('088645'),('089377'),('089342'),('089337'),
     ('089332'),('081635'),('089426'),('087197'),('089425'),('087767'),('088395'),('089341'),('089349'),('082114'),('082123'),
     ('084687'),('089333'),('089297'),('087371'),('089331')
) as t(id)
  left join users u on t.id = u.trader_systems_id 
where u.trader_systems_id  is null;

您还可以返回找到的和未找到的用户:

select t.id, 
       u.*,
from (
   values ( .... )
) as t(id)
  left join users u on t.id = u.trader_systems_id;

如果您获得包含逗号分隔ID的单个字符串,则可以使用unnest()string_to_array()将其转换为正确的ID:

select t.id, 
       u.*,
from unnest(string_to_array('089477,089485,089417,089419,089416,..', ',',  null)) as t(id)
  left join users u on t.id = u.trader_systems_id;

答案 1 :(得分:0)

根据您的评论:

select * from [given_list] EXCEPT select * from [already_in_database] 

这将返回一个列表但不包含另一个列表的用户。

显然,没有更多信息,很难使用主键,等等