You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Exists匹配记录(DBeaver):判断指定cust_nbr是否存在

问题分析与解决

你的SQL逻辑错误在于exists子查询没有和外部查询关联,导致只要目标列表里有任意一个cust_nbr存在于表中,所有行的EXIST_NOT都会显示True,无法实现每行判断自身cust_nbr是否在指定列表的需求。

正确写法1(最简洁)

直接用case when判断当前行的cust_nbr是否在目标列表中:

select 
    co_nbr, 
    cust_nbr,
    case when cust_nbr in ('015100','015105','015435','015589', '020085', '020184')
         then 'True'
         else 'False'
    end as EXIST_NOT
from sales_fw_co_cust

正确写法2(使用exists关联)

如果一定要用exists,需要在子查询中关联外部表的cust_nbr,确保每行判断自身的匹配情况:

select 
    co_nbr, 
    cust_nbr,
    case when exists (
        select 1 
        from sales_fw_co_cust s
        where s.cust_nbr = sales_fw_co_cust.cust_nbr
          and s.cust_nbr in ('015100','015105','015435','015589', '020085', '020184')
    )
         then 'True'
         else 'False'
    end as EXIST_NOT
from sales_fw_co_cust

两种写法都能实现需求,第一种更高效简洁,推荐使用。

内容的提问来源于stack exchange,提问作者Shobi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 11:12:43