为什么我可以引用SQL中外部查询中的字段
给定这些表格:为什么我可以引用SQL中外部查询中的字段,sql,Sql,给定这些表格: create temporary table a(pid integer, cid integer); create temporary table b(id integer, code varchar); 这可以工作,但返回错误的结果: select pid from a where cid in (select cid from b where code like 'AE%') 我只是有一个这样的查询,在那里我使用了错误的字段,这让我惊讶的是,该查询甚至可以工作。这样
create temporary table a(pid integer, cid integer);
create temporary table b(id integer, code varchar);
这可以工作,但返回错误的结果:
select pid
from a
where cid in (select cid from b where code like 'AE%')
我只是有一个这样的查询,在那里我使用了错误的字段,这让我惊讶的是,该查询甚至可以工作。这样的查询不是只返回表a中的所有行吗
您有没有像这样编写的查询示例有用
可能我遗漏了什么。您通常需要在内部查询的where子句中使用外部查询中的字段:
select * from a
where exists ( select 1 from b where id = cid );
或
我们还可以构造示例,其中外部字段在select子句中(某种程度上)很有用:
select * from a
where 1 = any (select id-cid from b where code like 'HE%');
因此,访问外部查询的字段是绝对必要的
select * from a
where 1 = any (select id-cid from b where code like 'HE%');