gpt4 book ai didi

sql - SQL server IN 子句的奇怪行为

转载 作者:行者123 更新时间:2023-12-01 09:25:57 25 4
gpt4 key购买 nike

我想知道为什么下面的 SQL 语句的行为方式是这样的:

select * 
from tableA
where document_id in (select <b>document_id</b>
from tableB
where batch_id = 99997)

tableA 包含列 document_idtableB 不包含,因此查询似乎返回 tableA 中的所有行>,如果您在 IN 子句的 select 语句中使用任何字段名称,该字段名称是 tableA 中的字段,则会发生此行为。使用不在 tableAtableB 中的名称会导致错误。

最佳答案

这不是错误。在子查询中,您仍然可以使用来自父查询的列。所以当你说

SELECT document_id FROM tableB WHERE batch_id = 99997

您是说对于 tableB 中 batch_id 为 9997 的每一行,从 tableA 中选择 document_id。当然,所有这些 document_id 值都存在,因此它将返回所有这些行。

关于sql - SQL server IN 子句的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24026060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com