混合“使用” 和“ON” 在 Oracle ANSI 中加入
我写了一个这样的 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:列定义不明确”。
解决这个问题的最佳方法是什么?
I wrote an Oracle SQL expression like this:
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)
When I try to run it, Oracle throws an error in the line with "ON" self-join saying: "ORA-25154: column part of USING clause cannot have qualifier".
If I omit the "am" qualifier, it says: "ORA-00918: column ambiguously defined".
What's the best way to resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
错误消息实际上(令人惊讶!)告诉您问题到底是什么。 一旦对特定列使用 USING 子句,就无法在查询的任何其他部分中对该列名使用列限定符/表别名。 解决此问题的唯一方法是不在查询中的任何位置使用 USING 子句,因为您必须在第二个连接条件上使用限定符:
The error message is actually (surprise!) telling you exactly what the problem is. Once you use the USING clause for a particular column, you cannot use a column qualifier/table alias for that column name in any other part of your query. The only way to resolve this is to not use the USING clause anywhere in your query, since you have to have the qualifier on the second join condition:
我的偏好是永远不要使用USING; 始终使用ON。 我喜欢我的 SQL 非常明确,并且我认为 USING 子句感觉被删除了一步。
在这种情况下,会出现错误,因为您在
mc_current_view
、account_master
和ml_client_account
中有account_no
,因此实际加入无法解决。 希望这可以帮助。My preference is never to use USING; always use ON. I like to my SQL to be very explicit and the USING clause feels one step removed in my opinion.
In this case, the error is coming about because you have
account_no
inmc_current_view
,account_master
, andml_client_account
so the actual join can't be resolved. Hope this helps.使用更干净(imo),但仍然需要外部引用连接字段,如组织示例或类似这样的示例:
它给出了错误的答案,因为子查询中的 join_id 暗示了 C.join_id (匹配所有记录)而不是或许最好的解决方法就是允许显式引用与使用,两全其美。 因为这样的情况,似乎有必要。
The using is cleaner (imo) but it is still desirable to externally refererence the join fields as in the org example or an example like this:
It gives the wrong answer because the join_id within the subquery implies C.join_id (matching all records) rather than A or B. Perhaps the best way to resolve might be just to allow explicit references with using, having the best of both worlds. Seems like there is a need because of cases like these.