我写了一个像这样的Oracle SQL表达式:
SELECT
...
FROM mc_current_view a
JOIN account_master am USING (account_no)
JOIN account_master am_loan ON (am.account_no = am_loan.parent_account_no)
JOIN ml_client_account mca USING (account_no)
当我尝试运行它时,Oracle在“ON”自连接行中抛出一个错误:“ORA-25154:USING子句的列部分不能有限定符”。
如果我省略“am”限定符,则会说:“ORA-00918:列模糊定义”。
解决此问题的最佳方法是什么?
答案 0 :(得分:13)
错误信息实际上是(惊喜!)告诉您究竟是什么问题。对特定列使用USING子句后,不能在查询的任何其他部分中为该列名使用列限定符/表别名。解决此问题的唯一方法是不在查询中的任何位置使用USING子句,因为必须在第二个连接条件上使用限定符:
SELECT
...
FROM mc_current_view a
JOIN account_master am ON (a.account_no = am.account_no)
JOIN account_master am_loan ON (am.account_no = am_loan.parent_account_no)
JOIN ml_client_account mca ON (a.account_no = mca.account_no);
答案 1 :(得分:8)
我的偏好是永远不要使用 USING ;始终使用开启。我喜欢我的SQL非常明确,而且 USING 条款在我看来感觉已经删除了一步。
在这种情况下,错误即将发生,因为account_no
,mc_current_view
和account_master
中有ml_client_account
,因此无法解析实际联接。希望这会有所帮助。
答案 2 :(得分:0)
使用更清晰(imo)但仍然需要外部参考联合字段,如组织示例或类似示例:
select A.field,
B.field,
(select count(C.number)
from tableC C
where C.join_id = join_id -- wrong answer w/o prefix, exception with.
) avg_number
from tableA A
join tableB B using (join_id);
它给出了错误的答案,因为子查询中的join_id意味着C.join_id(匹配所有记录)而不是A或B.也许最好的解决方法可能只是允许使用显式引用,两者都是最好的世界。由于这些情况,似乎有需要。