子查询具有无效的列名,但返回结果

问题描述 投票:-1回答:1

此查询中的子查询无效。程序表不包含名为“calcfile”的列。正确的列名是'calc_file'。但是,当我运行查询时,我没有得到任何错误,结果就好像where子句不存在一样。我不应该得到一个错误而不是无效的结果?

select distinct result from calcdetl
where calcfile = (select calcfile from program where program = 'HIGLAS Program')
order by result
;

https://snag.gy/StPnqf.jpg

sql sql-server
1个回答
4
投票

您的查询被解释为:

select distinct cd.result
from calcdetl cd
where cd.calcfile = (select cd.calcfile from program p where p.program = 'HIGLAS Program')
order by cd.result;

这是完全有效的 - 它是一个相关的子查询。

道德?当查询引用多个表时,始终使用限定列名。

如果你写了,你会得到一个错误:

select distinct cd.result
from calcdetl cd
where cd.calcfile = (select p.calcfile from program p where p.program = 'HIGLAS Program')
order by cd.result;
© www.soinside.com 2019 - 2024. All rights reserved.