混合“使用” 和“ON” 在 Oracle ANSI 中加入

发布于 2024-07-11 21:32:27 字数 401 浏览 8 评论 0原文

我写了一个这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冬天旳寂寞 2024-07-18 21:32:27

错误消息实际上(令人惊讶!)告诉您问题到底是什么。 一旦对特定列使用 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);

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:

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);
葬花如无物 2024-07-18 21:32:27

我的偏好是永远不要使用USING; 始终使用ON。 我喜欢我的 SQL 非常明确,并且我认为 USING 子句感觉被删除了一步。

在这种情况下,会出现错误,因为您在 mc_current_viewaccount_masterml_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 in mc_current_view, account_master, and ml_client_account so the actual join can't be resolved. Hope this helps.

颜漓半夏 2024-07-18 21:32:27

使用更干净(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 (匹配所有记录)而不是或许最好的解决方法就是允许显式引用与使用,两全其美。 因为这样的情况,似乎有必要。

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:

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);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文