如何根据我的选择是否存在于另一个表中来更改它的输出

问题描述 投票:0回答:1

我正在尝试选择某个名称是否已存在于我的客户表中。

这就是我想做的,但我收到错误“子选择不支持案例构造。感谢您的帮助。

select id, if(description not in (select name from customer where type in ('A', 'B','C')), 'NOT A CUSTOMER', description) from names
sql if-statement case
1个回答
0
投票

问题是,你的 sql 引擎告诉你的...尝试以下方法之一:

  1. 使用两个选择和联合。一项选择“仅限客户”,另一项不选择,

  2. 尝试在子查询中使用exists

  3. 连接表并具有可能为空值的列并在客户端处理它。

union(
    select id,
        c.description
        if(description not in (select name from customer where , 
    from names n
    left join customer c ON (c.id == n.customer_id)
    where n.type in ('A', 'B','C')
,
    select id,
        'NOT A CUSTOMER'
    from names n
    left join customer c ON (c.id == n.customer_id)
    where n.type not in ('A', 'B','C')
)

PS:在发布之前格式化你的sql,包括野兽答案的逻辑模型并提及,你正在使用什么数据库。

© www.soinside.com 2019 - 2024. All rights reserved.