查询/子查询什么时候返回 NULL,什么时候根本没有值?
如果查询/子查询没有找到任何匹配的行,那么它要么返回 NULL,要么根本不返回任何值,因此甚至不是 NULL 值。基于 查询/子查询返回 NULL 的条件是什么?何时不返回任何结果,甚至不返回 NULL 值?
当没有时,标量子查询是否总是返回 NULL 找到匹配的行?我假设最外层标量查询也会返回 如果没有找到行,则返回 NULL?
选择名字、姓氏、年份(出生日期) 来自人 WHERE YEAR(出生日期)IN(从专辑中选择发行年份);
如果子查询没有找到结果,则为外层的WHERE子句 查询翻译为
WHERE YEAR(BirthDate) IN (null);
?如果 WHERE 子句被翻译为
WHERE YEAR(BirthDate) IN();
,这不应该是一个错误条件,因为YEAR(BirthDate)< /code> 价值无以伦比?
If a query/subquery doesn’t find any matching rows, then it either returns NULL or no value at all, thus not even a NULL value. Based on
what criteria does a query/subquery return a NULL and when doesn’t it return any results, not even a NULL value?Will a scalar subquery always return NULL, when no
matching rows are found? I assume most-outer scalar query also returns
NULL if no rows are found?SELECT FirstName, LastName, YEAR(BirthDate) FROM Persons WHERE YEAR(BirthDate) IN (SELECT YearReleased FROM Albums);
If the subquery finds no results, is the WHERE clause of the outer
query translated intoWHERE YEAR(BirthDate) IN (null);
?If WHERE clause is translated into
WHERE YEAR(BirthDate) IN();
instead, shouldn’t that be an error condition, since how canYEAR(BirthDate)
value be compared to nothing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅当
YearReleased
为NULL
时,子查询才会返回NULL
,否则将出现一个空记录集,使其成为IN ()
你提到的案例。区分两者非常重要,因为它们意味着完全不同的事物。
NULL
表示有一些内容需要进行SELECT
编辑,尽管该值可以说表示“缺乏价值”。空记录集表示没有任何与指定条件相匹配的可供选择的记录集。编辑:更新以显示示例结果
前两个查询只是为了显示其中的内容两张桌子。第三个查询是您的查询,第四个查询只是显示,如果您用
NULL
替换子查询,它会产生等效结果(没有行)。最后一个查询只是为了表明子查询本身只返回一个大的 NULL 列表。The subquery would only ever return
NULL
whenYearReleased
wasNULL
, otherwise there would be an empty recordset, making it theIN ()
case you mentioned.It's very important to distinguish between the two as they mean entirely different things.
NULL
indicates that there was something to beSELECT
ed, although that value indicates a "lack of value" so to speak. An empty recordset indicates that there was nothing to be selected that matched the criteria specified.EDIT: updated to show example results
First two queries are just to show what's in the two tables. Third query is your query and the fourth query just shows that it produces an equivalent result (no rows) if you replace the subquery with a
NULL
. Last query is just to show that the subquery itself just returns a big list ofNULL
s.一个。如果没有匹配的行,则结果集将始终为空。对于 NULL 值没有任何特殊处理。
b.那不是真的。如果没有匹配的行,则根据定义,结果集始终为空。标量函数的结果不是结果集,因此它将为 NULL 或其他值。
c.1.如果子查询不返回任何行,则“IN”表达式将始终返回 false。该集合不会是
NULL
。c.2.将
YEAR(BirthDate)
与空集进行比较是有效的。它总是返回 false。a. If there are no matching rows, then the result set will always be empty. There isn't any special handling for the NULL value.
b. That's not true. If there are no matching rows, then the result set is always empty by definition. The result of a scalar function is not a result set so it will either be NULL or another value.
c.1. If the subquery doesn't return any rows then the "IN" expression will always return false. The set will not be
NULL
.c.2. It is valid to compare
YEAR(BirthDate)
with an empty set. It will just always return false.SQL 中有多种类型的子查询,您获得哪一种(以及结果行为)取决于上下文。例如,如果使用
(subquery)
语法作为大多数表达式上下文中的操作数,则这是标量子查询。另一方面,作为 IN 运算符的右操作数,它是一个表子查询。对于标量子查询:
如果子查询未返回任何行,则标量子查询的结果为
NULL
。如果子查询返回多于一行,则会出错。
如果子查询返回一行,则结果是该行的查询(唯一)列的值。
对于表子查询:
如果子查询不返回任何行,则结果是一组空值,因此例如
IN
操作将始终返回 false。否则,结果是所有行的查询列的集合。
因此,要单独解决您的问题:
这取决于子查询的上下文
是的,如果存在,标量子查询总是返回
NULL
没有行发现
在
IN
操作的上下文中它是一个表子查询并且所以没有行意味着没有可匹配的值。但请注意,由于
如果您的查询返回查询所在的行,则为
IN
的语义列值本身就是
NULL
,您的代码中将有一个NULL
比较集。
IN
运算实际上是相等的OR
左操作数与每个值之间的比较
右侧的值集。集合中的空值将导致
“未知”而不是 true 或 false,因此
IN
操作可以只返回 true (如果存在与某个非空值匹配的情况)
集合)或未知(因为该值可能或可能不匹配 null)。
There are several types of subquery in SQL, and which one you get (and the resulting behaviour) depends on the context. If the
(subquery)
syntax is used, say, as an operand in most expression contexts, this is a scalar subquery. On the other hand, as the right operand of theIN
operator, it is a table subquery.For a scalar subquery:
If the subquery returns no rows, the result of the scalar subquery is
NULL
.If the subquery returns more than one row, it is an error.
If the subquery returns one row, the result is the value of the query's (only) column for that row.
For a table subquery:
If the subquery returns no rows, the result is an empty set of values, so for instance the
IN
operation would always return false.Otherwise, the result is the set of the query's column for all the rows.
So to address your questions individually:
It depends on the context of the subquery
Yes, a scalar subquery always returns
NULL
if there are no rowsfound
In the context of the
IN
operation it is a table subquery andso no rows means no values to match against. Note however that, due
to the semantics of
IN
if your query returns a row where the querycolumn value is itself
NULL
, you will have aNULL
in yourcompare set. The
IN
operation is effectively theOR
of equalitycomparisons between the left operand and each value individually in
the right-hand value set. Null values in the set will result in
"unknown" rather than true or false, and so the
IN
operation canonly ever return true (if there is a match to some non-null value in
the set) or unknown (because the value may or may not match null).