【问题标题】:Return names which dont exist in the database from the list provided从提供的列表中返回数据库中不存在的名称
【发布时间】:2023-02-02 23:38:26
【问题描述】:

假设我有一个公司名称列表,如下所示:

CompA
CompB
CompC

我将如何只返回数据库中不存在的名称。

SELECT * FROM db.companies dc WHERE dc.name NOT IN ('CompA','CompB','CompC')

我试过使用NOT EXISTSNOT IN,但这会返回所有不在列表中但存在于数据库中的公司名称,但我只需要指定列表中不存在的名称。

因此,例如,如果 CompC 不是现有公司,它应该只返回 CompC

【问题讨论】:

    标签: sql amazon-redshift dbeaver


    【解决方案1】:

    将您的公司列表制作成表格,然后从中查询。

    create temp table tmp_companies (name varchar(100)); 
    insert into tmp_companies 
    values 
    ('CompA'), 
    ('CompB'), 
    ('CompC'); 
    
    select * 
    from tmp_companies c
    where not exist (
    select 1 
    from db.companies dc
    where dc.name = c.name
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 2017-06-01
      相关资源
      最近更新 更多