列不明确的 SQL

标签 sql oracle

检查以下示例(Oracle 语法):

create table t1 (id number, a varchar2(255));
create table t2 (id number, a varchar2(255));
create table t3 (id number);

select * from t1 where id in (select id from t2 where a = 'aa');
select * from t1 where id in (select id from t3 where a = 'aa');

两者都选择工作正常,但它们使用不同的属性进行过滤。在我看来,第一个 SQL 应该给出一个错误,因为列 a 的定义不明确。这在官方 SQL 标准中没有定义吗?

最佳答案

这不是最清楚的说法,而是the documentation does refer to this behaviour :

If columns in a subquery have the same name as columns in the containing statement, then you must prefix any reference to the column of the table from the containing statement with the table name or alias. To make your statements easier to read, always qualify the columns in a subquery with the name or alias of the table, view, or materialized view.



所以基本上这就是说你的第一个声明要引用 t1.a您必须明确使用它的名称(或别名):
select * from t1 where id in (select id from t2 where t1.a = 'aa');

如果您不这样做,则默认情况下它将使用子查询的表。这对解析器来说并没有歧义,因为它会首先查看(子)查询的同一级别中的表,并且如果在当前级别中找不到该列,则仅查看外部级别。这可能不是你所期望的,我同意提示它是模棱两可的,最好避免产生错误结果的细微错误;但它是如何工作的。

但正如它也所说,无论如何总是明确的更安全:
select * from t1 where t1.id in (select t2.id from t2 where t2.a = 'aa');

或者
select * from t1 where t1.id in (select t2.id from t2 where t1.a = 'aa');

对于你的第二个陈述:
select * from t1 where t1.id in (select t3.id from t3 where t1.a = 'aa');

关于列不明确的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39871799/

相关文章:

MySQL 插入不正确的十进制值

mysql - 将文本插入到恰好包含 MySQL 中的 sql 的 varchar 中?

mysql - 使用多个表的 SQL SELECT

SQL - MIN 和 COUNT 的组合

sql - 带有 GROUP BY 的 3 个 SELECT 语句的平均值

sql - 使用oracle sql列出所有月份

sql - 打印具有空值的列名 oracle

mysql - 检索用户好友的帖子,每个好友只有一行

asp.net - 如何在 x64 中使用 WebDev.WebServer.exe (VS Web Server)?

java - JPA - EclipseLink - 如何更改默认模式