Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 比较同一表中的两列值_Sql_Oracle - Fatal编程技术网

Sql 比较同一表中的两列值

Sql 比较同一表中的两列值,sql,oracle,Sql,Oracle,我有一个包含以下内容的表格: a|b|c -+-+- 1|2|3 2|2|2 3|2|4 4|4|4 5|6|7 我想检查中的所有值是否相同,如果相同,则打印一条消息。 示例:行4,4,4和2,2,2具有相同的列值。所以我想打印“相同”或“不同” 如何做到这一点?当 select case when (a=b and a=c ) then 'same' else 'Different' end as output from t 使用时的用例 select case when

我有一个包含以下内容的表格:

a|b|c
-+-+-
1|2|3
2|2|2
3|2|4
4|4|4
5|6|7
我想检查中的所有值是否相同,如果相同,则打印一条消息。 示例:行4,4,4和2,2,2具有相同的列值。所以我想打印“相同”或“不同”

如何做到这一点?

    select case when (a=b and a=c ) then 'same' else 'Different' end as output
from t
使用时的用例

    select case when (a=b and a=c ) then 'same' else 'Different' end as output
from t

用这个。请注意,
a=b和a=c
也意味着
b=c
,因此您只需要两个比较

select a,b,c, case when (a=b and a=c) then 'SAME' else 'Different' end as print
from tableName
编辑:当a、b、c均为
null
时,情况就不同了。如果希望将所有3个值均为
null
的行视为相同,可以添加
或(a为空,b为空,c为空)
或coalesce(a,b,c)为空
。 像这样:

select a,b,c, 
case when ((a=b and a=c) or(a is null and b is null and c is null)) then 'SAME' else 'Different' end as print
from tableName


用这个。请注意,
a=b和a=c
也意味着
b=c
,因此您只需要两个比较

select a,b,c, case when (a=b and a=c) then 'SAME' else 'Different' end as print
from tableName
编辑:当a、b、c均为
null
时,情况就不同了。如果希望将所有3个值均为
null
的行视为相同,可以添加
或(a为空,b为空,c为空)
或coalesce(a,b,c)为空
。 像这样:

select a,b,c, 
case when ((a=b and a=c) or(a is null and b is null and c is null)) then 'SAME' else 'Different' end as print
from tableName


如果您有多个非空列,那么一个非常通用的方法是:

select t.*,
       (case when least(a, b, c) = greatest(a, b, c) then 'SAME' else 'DIFFERENT' end) as flag
from t;

这很容易推广到更多的列。

如果您有多个非空列,那么一个非常通用的方法是:

select t.*,
       (case when least(a, b, c) = greatest(a, b, c) then 'SAME' else 'DIFFERENT' end) as flag
from t;

这很容易推广到更多的专栏。

您没有接受上一个问题的答案。如果这是一个有效的答案,请这样做。谢谢一匹没有名字的马,非常好用!你没有接受上一个问题的答案。如果这是一个有效的答案,请这样做。谢谢一匹没有名字的马,非常好用!这会在Oracle上运行吗?这会在Oracle上运行吗?回答得好。虽然比较的次数多,但这是一个更好的答案。虽然比较的次数更多