gpt4 book ai didi

mysql CASE WHEN 要么但不是两者

转载 作者:行者123 更新时间:2023-11-29 01:19:59 25 4
gpt4 key购买 nike

我有一个案例陈述:

case when R1 = risk_factor OR R2 = risk_factor then 1 else 0 end

但它也开始了

case when R1 = risk_factor AND R2 = risk_factor then 1 else 0 end

陈述的正确语法是什么:

case when (R1 = risk_factor or R2 = risk_factor) BUT NOT (R1 = risk_factor AND R2 = risk_factor) then 1 else 0 end   

最佳答案

(R1 = risk_factor or R2 = risk_factor) BUT NOT (R1 = risk_factor AND R2 = risk_factor)

应该是

(R1 = risk_factor or R2 = risk_factor) AND NOT (R1 = risk_factor AND R2 = risk_factor)

但是你也可以使用XOR

(R1 = risk_factor XOR R2 = risk_factor)

Logical XOR. Returns NULL if either operand is NULL. For non-NULL operands, evaluates to 1 if an odd number of operands is nonzero, otherwise 0 is returned.

Documentation

但是-在您的特定情况下,您也可以将条件写为

(risk_factor IN (R1, R2) AND R1 <> R2)

注意:如果您的 CASE 表达式类似于 then 1 else 0,那么您根本不需要它。您可以只使用 bool 表达式。但有一个异常(exception):如果您的 bool 表达式可以返回 NULL,而您想将其转换为 0。但在那种情况下,您也可以使用 COALESCE(boolean_expression, 0)

关于mysql CASE WHEN 要么但不是两者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43424749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com