PostgreSQL:使用大小写比较列之间的值

PostgreSQL: using case to compare values between columns

我想比较两列,A 和 B。

如果 Col A 和 Col B 共享相同的值,我希望 Col A 没有设置值。 如果 Col A 和 Col B 不共享相同的值,我希望 Col A 保留它的原始值。

以下是我的尝试。但是,它 returns 对于案例列中的所有条目都是“真” - Col A(已更正)。

select *,
    case when 'Col A' = 'Col B' 
        then 'Col A' = null 
        else 'Col A' = 'Col A'
        end as "Col A (corrected)"
from my_table
select
  case
   when col_a=col_b then null
   else col_a
 end as col_a_corrected
from my_table

简单做

select *,
       NULLIF(col_a, col_b)
from my_table

如果col_a=col_b,NULLIFreturnsnull,否则返回col_a。

https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-NULLIF