Oracle使用+;子查询:ora-00904字符串:无效标识符
我的查询有一点语法问题(简化): 通过使用“using”关键字,oracle不允许在列名前面使用表标识符(例如:t1.pk1,只能使用pk1) 如果我写:Oracle使用+;子查询:ora-00904字符串:无效标识符,oracle,syntax,join,exists,ora-00904,Oracle,Syntax,Join,Exists,Ora 00904,我的查询有一点语法问题(简化): 通过使用“using”关键字,oracle不允许在列名前面使用表标识符(例如:t1.pk1,只能使用pk1) 如果我写: select * from table1 t1 inner join table2 t2 using (pk1) inner join table3 t3 using (pk2) where not exists (select1 from table4 t4 where t4.pk1 = pk1) 此查询不会给出预期的结果 但是,既然我使
select *
from table1 t1
inner join table2 t2 using (pk1)
inner join table3 t3 using (pk2)
where not exists (select1 from table4 t4 where t4.pk1 = pk1)
此查询不会给出预期的结果
但是,既然我使用的是“exists”子查询,那么如何加入这个子查询呢
当然,我想我可以用另一种方式编写这个查询,避免使用exists,或者我不能使用“using”
但是,在where子句中是否可以将“join/using”与子查询结合起来
编辑:使用Oracle 10gR2时,不能将表限定符与自然联接一起使用 此查询:
select 1 from table4 t4 where t4.pk1 = pk1
正在被解析为
select 1 from table4 t4 where t4.pk1 = t4.pk1
而不存在
如果表4中只有一条记录
,则它总是返回false
只需使用显式的JOIN
条件:
WITH table1 AS
(
SELECT 1 AS pk1
FROM dual
),
table2 AS
(
SELECT 1 AS pk1, 1 AS pk2
FROM dual
),
table3 AS
(
SELECT 1 AS pk2
FROM dual
),
table4 AS
(
SELECT 2 AS pk1
FROM dual
)
SELECT *
FROM table1 t1
JOIN table2 t2
ON t2.pk1 = t1.pk1
JOIN table3 t3
ON t3.pk2 = t2.pk2
WHERE NOT EXISTS
(
SELECT 1
FROM table4 t4
WHERE t4.pk1 = t1.pk1
)
有趣的问题!在继续使用时,我所能管理的最好方法是:
select * from
( select *
from table1 t1
inner join table2 t2 using (pk1)
inner join table3 t3 using (pk2)
) v
where not exists (select1 from table4 t4 where t4.pk1 = v.pk1)
实际上,这是在不完全避免使用的情况下做到这一点的唯一方法(我个人更喜欢坚持加入..ON而不是使用)。
select * from
( select *
from table1 t1
inner join table2 t2 using (pk1)
inner join table3 t3 using (pk2)
) v
where not exists (select1 from table4 t4 where t4.pk1 = v.pk1)