Oracle SQL 语法:内连接

发布于 2024-11-06 08:25:25 字数 178 浏览 6 评论 0原文

我现在无法访问 Oracle 数据库,因此我在这里发布我的问题:

以下语句是否有效 Oracle SQL 语法?

SELECT a1
FROM t1 INNER JOIN t2

我特别想知道我们是否需要为内部联接指定联接属性。

最好的, 将要

I don't have access to an Oracle Database right now, so I'm posting my question here:

Is the following statement valid Oracle SQL Syntax?

SELECT a1
FROM t1 INNER JOIN t2

I'm particularly wondering whether we need to specify a join attribute for the inner join.

Best,
Will

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

乖乖哒 2024-11-13 08:25:25

您缺少 ON

Like

SELECT a1
FROM t1 INNER JOIN t2
ON t1.SomeID = t2.SomeID

You're missing ON

Like

SELECT a1
FROM t1 INNER JOIN t2
ON t1.SomeID = t2.SomeID
带上头具痛哭 2024-11-13 08:25:25

所以,这就是您正在考虑的查询......

SQL> select e.ename
  2         , d.dname
  3  from emp e inner join dept d
  4  /
from emp e inner join dept d
                           *
ERROR at line 3:
ORA-00905: missing keyword


SQL>

正如我们所看到的,它失败了。 INNER JOIN 语法要求我们提供要连接的列...

SQL> select e.ename
  2         , d.dname
  3  from emp e inner join dept d
  4      on ( d.deptno = e.deptno )
  5  /

ENAME      DNAME
---------- --------------
SCHNEIDER  ACCOUNTING
BOEHMER    ACCOUNTING
KISHORE    ACCOUNTING
ROBERTSON  RESEARCH
...
FEUERSTEIN HOUSEKEEPING
PODER      HOUSEKEEPING
TRICHLER   HOUSEKEEPING

21 rows selected.

SQL>

还有一种替代语法,即 NATURAL JOIN。此语法将根据共享相同名称的所有列自动连接两个表。

SQL> select e.ename
  2         , d.dname
  3  from emp e natural join dept d
  4  /

ENAME      DNAME
---------- --------------
SCHNEIDER  ACCOUNTING
BOEHMER    ACCOUNTING
KISHORE    ACCOUNTING
ROBERTSON  RESEARCH
...
FEUERSTEIN HOUSEKEEPING
PODER      HOUSEKEEPING
TRICHLER   HOUSEKEEPING

21 rows selected.

SQL>

这是一个巧妙的技巧,但实际上不应该在生产代码中依赖它;这是一个等待发生的错误。

So, this is the query you're thinking of....

SQL> select e.ename
  2         , d.dname
  3  from emp e inner join dept d
  4  /
from emp e inner join dept d
                           *
ERROR at line 3:
ORA-00905: missing keyword


SQL>

As we can see, it fails. The INNER JOIN syntax demands that we provide columns to join on ...

SQL> select e.ename
  2         , d.dname
  3  from emp e inner join dept d
  4      on ( d.deptno = e.deptno )
  5  /

ENAME      DNAME
---------- --------------
SCHNEIDER  ACCOUNTING
BOEHMER    ACCOUNTING
KISHORE    ACCOUNTING
ROBERTSON  RESEARCH
...
FEUERSTEIN HOUSEKEEPING
PODER      HOUSEKEEPING
TRICHLER   HOUSEKEEPING

21 rows selected.

SQL>

There is an alternative syntax, the NATURAL JOIN. This syntax will automatically join the two tables on the basis of all columns which share the same name.

SQL> select e.ename
  2         , d.dname
  3  from emp e natural join dept d
  4  /

ENAME      DNAME
---------- --------------
SCHNEIDER  ACCOUNTING
BOEHMER    ACCOUNTING
KISHORE    ACCOUNTING
ROBERTSON  RESEARCH
...
FEUERSTEIN HOUSEKEEPING
PODER      HOUSEKEEPING
TRICHLER   HOUSEKEEPING

21 rows selected.

SQL>

This is a neat trick but really shouldn't be relied upon in production code; it is a bug waiting to happen.

北方。的韩爷 2024-11-13 08:25:25

您需要添加 ON 子句

SELECT a1
FROM t1 INNER JOIN t2 on t1.a1=t2.a1

You will need to add an ON clause

SELECT a1
FROM t1 INNER JOIN t2 on t1.a1=t2.a1
花落人断肠 2024-11-13 08:25:25

是的,您必须指定连接条件:

FROM t1 INNER JOIN t2 on t1.f = t2.f

Yes, you have to specify join condition:

FROM t1 INNER JOIN t2 on t1.f = t2.f

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