查询/子查询什么时候返回 NULL,什么时候根本没有值?

发布于 2024-08-30 07:25:27 字数 556 浏览 4 评论 0原文

  1. 如果查询/子查询没有找到任何匹配的行,那么它要么返回 NULL,要么根本不返回任何值,因此甚至不是 NULL 值。基于 查询/子查询返回 NULL 的条件是什么?何时不返回任何结果,甚至不返回 NULL 值?

  2. 当没有时,标量子查询是否总是返回 NULL 找到匹配的行?我假设最外层标量查询也会返回 如果没有找到行,则返回 NULL?

  3. 选择名字、姓氏、年份(出生日期)
    来自人
    WHERE YEAR(出生日期)IN(从专辑中选择发行年份);
    
    • 如果子查询没有找到结果,则为外层的WHERE子句 查询翻译为 WHERE YEAR(BirthDate) IN (null);?

    • 如果 WHERE 子句被翻译为 WHERE YEAR(BirthDate) IN();,这不应该是一个错误条件,因为 YEAR(BirthDate)< /code> 价值无以伦比?

  1. 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?

  2. 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?

  3. 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 into WHERE YEAR(BirthDate) IN (null);?

    • If WHERE clause is translated into WHERE YEAR(BirthDate) IN(); instead, shouldn’t that be an error condition, since how can YEAR(BirthDate) value be compared to nothing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我很OK 2024-09-06 07:25:27

仅当 YearReleasedNULL 时,子查询才会返回 NULL,否则将出现一个空记录集,使其成为 IN () 你提到的案例。

区分两者非常重要,因为它们意味着完全不同的事物。 NULL 表示有一些内容需要进行 SELECT 编辑,尽管该值可以说表示“缺乏价值”。空记录集表示没有任何与指定条件相匹配的可供选择的记录集。

编辑:更新以显示示例结果

alt text

前两个查询只是为了显示其中的内容两张桌子。第三个查询是您的查询,第四个查询只是显示,如果您用 NULL 替换子查询,它会产生等效结果(没有行)。最后一个查询只是为了表明子查询本身只返回一个大的 NULL 列表。

The subquery would only ever return NULL when YearReleased was NULL, otherwise there would be an empty recordset, making it the IN () 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 be SELECTed, 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

alt text

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 of NULLs.

骷髅 2024-09-06 07:25:27

一个。如果没有匹配的行,则结果集将始终为空。对于 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.

一花一树开 2024-09-06 07:25:27
SELECT FirstName, LastName, YEAR(BirthDate)
FROM Persons a
left join (SELECT YearReleased FROM Albums) b on year(b.YearReleased) = year(a.BirthDate)
where year(b.YearReleased) = year(a.BirthDate)
SELECT FirstName, LastName, YEAR(BirthDate)
FROM Persons a
left join (SELECT YearReleased FROM Albums) b on year(b.YearReleased) = year(a.BirthDate)
where year(b.YearReleased) = year(a.BirthDate)
韬韬不绝 2024-09-06 07:25:27

SQL 中有多种类型的子查询,您获得哪一种(以及结果行为)取决于上下文。例如,如果使用 (subquery) 语法作为大多数表达式上下文中的操作数,则这是标量子查询。另一方面,作为 IN 运算符的右操作数,它是一个表子查询。

对于标量子查询:
如果子查询未返回任何行,则标量子查询的结果为 NULL
如果子查询返回多于一行,则会出错。
如果子查询返回一行,则结果是该行的查询(唯一)列的值。

对于表子查询:
如果子查询不返回任何行,则结果是一组空值,因此例如 IN 操作将始终返回 false。
否则,结果是所有行的查询列的集合。

因此,要单独解决您的问题:

  1. 这取决于子查询的上下文

  2. 是的,如果存在,标量子查询总是返回NULL没有行
    发现

  3. 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 the IN 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:

  1. It depends on the context of the subquery

  2. Yes, a scalar subquery always returns NULL if there are no rows
    found

  3. In the context of the IN operation it is a table subquery and
    so 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 query
    column value is itself NULL, you will have a NULL in your
    compare set. The IN operation is effectively the OR of equality
    comparisons 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 can
    only 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文