这个 SQL 查询是什么意思?
我有以下 SQL 查询:
select AuditStatusId
from dbo.ABC_AuditStatus
where coalesce(AuditFrequency, 0) <> 0
我有点难以理解它。它看起来很简单,我知道合并运算符的作用(或多或少),但似乎不明白其含义。
除了上面的查询之外,不知道更多信息,您认为这意味着什么?
I have the following SQL query:
select AuditStatusId
from dbo.ABC_AuditStatus
where coalesce(AuditFrequency, 0) <> 0
I'm struggling a bit to understand it. It looks pretty simple, and I know what the coalesce operator does (more or less), but dont' seem to get the MEANING.
Without knowing anymore information except the query above, what do you think it means?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请注意,使用 Coalesce 意味着将无法正确使用索引来满足此查询。
Note that the use of Coalesce means that it will not be possible to use an index properly to satisfy this query.
COALESCE 是处理 NULL 值的 ANSI 标准函数,它根据逗号分隔列表返回第一个非 NULL 值。这:
..意味着如果
AuditFrequency
列为 NULL,则将该值转换为零。否则,将返回AuditFrequency
值。由于比较的目的是不返回
AuditFrequency
列值为零的行,因此AuditFrequency
为 NULL 的行也将被查询忽略。COALESCE is the ANSI standard function to deal with NULL values, by returning the first non-NULL value based on the comma delimited list. This:
..means that if the
AuditFrequency
column is NULL, convert the value to be zero instead. Otherwise, theAuditFrequency
value is returned.Since the comparison is to not return rows where the
AuditFrequency
column value is zero, rows whereAuditFrequency
is NULL will also be ignored by the query.看起来它的设计目的是将 null AuditFrequency 检测为零,从而隐藏这些行。
It looks like it's designed to detect a null AuditFrequency as zero and thus hide those rows.
据我所见,它检查不为 0 或 null 的字段。
From what I can see, it checks for fields that aren't 0 or null.
我认为这样描述更准确:
我承认最后一部分永远不会做任何事情,也许我只是迂腐,但对我来说,这更准确地描述了您的查询。
I think it is more accurately described by this:
I'll admit the last part will never do anything and maybe i'm just being pedantic but to me this more accurately describes your query.
这个想法是希望使用单个表达式来表达单个搜索条件,但这只是风格,品味问题:
一个表达式:
两个表达式:
这是另一个例子:
一个表达式:
两个表达式
就我个人而言,我有很强的个人偏好对于单个表达式并发现它们更容易阅读...如果我熟悉所使用的模式;)它们是否表现不同是另一回事...
The idea is that it is desireable to express a single search condition using a single expression but it's merely style, a question of taste:
One expression:
Two expressions:
Here's another example:
One expression:
Two expressions
Personally, I have a strong personal perference for single expressions and find them easier to read... if I am familiar with the pattern used ;) Whether they perform differently is another matter...