比较SQL case语句中的3个变量

比较SQL case语句中的3个变量,sql,sql-server,Sql,Sql Server,我正在比较表中的3个变量值,因为我正在使用case语句 这是桌子的图像 我尝试的是这个SQL选择查询 select a,b,c, case( when (a=b=c) then "Equilateral" when a!=b!=c then "Scalene" when a=b!=c or a!=b=c then "Isosceles" else "Not A Triangle" end as Text ) from TRIANGLES; 我得到的错误

我正在比较表中的3个变量值,因为我正在使用case语句

这是桌子的图像

我尝试的是这个SQL选择查询

select a,b,c,
case(
    when (a=b=c) then "Equilateral"
    when a!=b!=c then "Scalene"
    when a=b!=c or a!=b=c then "Isosceles"
    else "Not A Triangle"
    end as Text
)
from TRIANGLES;
我得到的错误如下图所示


有人能告诉我我做错了什么吗?

你不能像
a=b=c
那样链接比较,你需要明确地分别编写它们并合并它们的结果:

when (a = b AND a = c) then "Equilateral"
when (a != b AND b != c AND c != a) then "Scalene"

诸如此类。

您不能像
a=b=c
那样链接比较,您需要分别显式地编写它们并合并它们的结果:

when (a = b AND a = c) then "Equilateral"
when (a != b AND b != c AND c != a) then "Scalene"

等等。

重写表达式,这是不受支持的

select a,b,c,
case
    when (a=b) and (b=c) then 'Equilateral'
    when (a!=b) and (b!=c) then 'Scalene'
    when (a=b and b!=c) or (a!=b and b=c) then 'Isosceles'
    else 'Not A Triangle'
    end as [Text]

from triangles;

重写您的表达式,这是不受支持的

select a,b,c,
case
    when (a=b) and (b=c) then 'Equilateral'
    when (a!=b) and (b!=c) then 'Scalene'
    when (a=b and b!=c) or (a!=b and b=c) then 'Isosceles'
    else 'Not A Triangle'
    end as [Text]

from triangles;

一次不能比较3个操作数。您需要使用and运算符拆分它

select a,b,c,
case(
   when (a=b and b=c) then 'Equilateral'
   when (a!=b and b!=c and c!=a) then 'Scalene'
   when (a=b and b!=c) or (a!=b and b=c) then 'Isosceles'
   else 'Not A Triangle' 
     end as Text
   )
from TRIANGLES;

一次不能比较3个操作数。您需要使用and运算符拆分它

select a,b,c,
case(
   when (a=b and b=c) then 'Equilateral'
   when (a!=b and b!=c and c!=a) then 'Scalene'
   when (a=b and b!=c) or (a!=b and b=c) then 'Isosceles'
   else 'Not A Triangle' 
     end as Text
   )
from TRIANGLES;

我想你不应该用括号来包装
WHEN
子句。我第一次尝试不使用括号也不起作用。你收到了来自MS SQL Server的Oracle错误消息?@jarlh我在Hackersrank上发布了我的答案,所以这只是Oracle的标签,否则我使用的是MS SQL Server。我想你不应该使用用括号括住
WHEN
子句。我第一次尝试不使用括号也不起作用。你从MS SQL Server收到Oracle错误消息?@jarlh我在Hackersrank上发布了我的答案,所以这只是Oracle的选项卡,否则我使用的是MS SQL Server。再次出现此错误。当(a=b)和(b=c)时,则第3行出现“等边”错误:ORA-00907:右侧缺失parenthesis@SyedArsalanHussain,2个问题,在字符串值上使用单引号,然后
Text
是保留关键字,因此您需要重新包装感谢:)再次获取此错误。当(a=b)和(b=c)时,则第3行出现“等边”错误:ORA-00907:右侧缺失parenthesis@SyedArsalanHussain,2个问题,对字符串值使用单引号,然后
Text
是保留关键字,因此您需要包装感谢:)