gpt4 book ai didi

sql - 在 SQL 中使用 CASE 语句时不等于

转载 作者:行者123 更新时间:2023-11-29 14:10:57 30 4
gpt4 key购买 nike

在 postgresql 中,我有一个 case我需要添加“不等于”子句的声明。

v1等于 v2,我希望它说 1,当 v1不等于 v2 , 我想说 2.

create table test (
v1 varchar(20),
v2 varchar(20)
);

insert into test values ('Albert','Al'),('Ben','Ben')

select case v1
when v2 then 1
else 3
end
from test

我尝试使用 !=<> ,但这似乎不起作用。

有谁知道如何在 case 中使用不等于? SQL 语句?

最佳答案

你总是可以用你的案例陈述更明确。这是一个例子...

    select 
case when v1 = v2 then 1
when v1 <> v2 then 2
end
from test

关于sql - 在 SQL 中使用 CASE 语句时不等于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31062388/

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