Postgres CASE 表达式然后 null

huangapple go评论67阅读模式
英文:

Postgres CASE expression then null

问题

The Chinese translation of the provided content is as follows:

以下查询是我在我的应用程序中获得的选择查询的一部分

其中
如果 o.tracking_id <> '0' 则 a.account_id 为 null

o.status = 'N'

否则

o.tracking_id = '0'

o.status = 'N'

我很难理解下面这行的意思

你能告诉我这到底是什么意思吗?

case when o.tracking_id <> '0' then a.account_id is null

英文:

The below query is part of a select query which I have got in my application

where 
case when o.tracking_id &lt;&gt; &#39;0&#39; then a.account_id is null
and 
o.status = &#39;N&#39;

else
(

o.tracking_id = &#39;0&#39;
and 
o.status = &#39;N&#39;

)
END

I am having hardtime to understand the below line

Can you please tell me what does this exactly mean ?

case when o.tracking_id &lt;&gt; &#39;0&#39; then a.account_id is null

答案1

得分: 0

我不会在这里使用 CASE 表达式,而是会使用以下逻辑:

WHERE
    (o.tracking_id <> '0' AND a.account_id IS NULL AND o.status = 'N')
    OR
    (o.tracking_id = '0' AND o.status = 'N')
英文:

I wouldn't use a CASE expression here but instead would use the following logic:

<!-- language: sql -->

WHERE
    (o.tracking_id &lt;&gt; &#39;0&#39; AND a.account_id IS NULL AND o.status = &#39;N&#39;)
    OR
    (o.tracking_id = &#39;0&#39; AND o.status = &#39;N&#39;)

答案2

得分: 0

条件写得有些笨拙。等价于这个更易读的表达式:

where (o.tracking_id = '0' or a.account is null)
and o.status = 'N';
英文:

The condition is written somewhat clumsily. It is equivalent to this more readable expression:

where (o.tracking_id = &#39;0&#39; or a.account is null)
and o.status = &#39;N&#39;;

huangapple
  • 本文由 发表于 2023年1月6日 10:49:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定