如何在oracle中对两个表执行别名连接?

Rol*_*ndo 3 sql oracle

我有以下问题。我有 2 个表想要加入“TableALong”和“TableBLong”

TableALong

ID, Name, Store,Age
1, John, Walmart, 5
2, Johnny, Walmart, 8
3, Johnny, Target , 10
4, Bill, Shoppers, 2
5, Joe, Target, 3

TableBLong
ID, Name, Store, StoreAddress
1, John, Walmart, 35353 Address
1, John, Walmart, 53544 Address
2, Johnny, Walmart, 35353 Address
Run Code Online (Sandbox Code Playgroud)

在加入之前,我想做一些类似 ALIAS 的事情,我有这样的事情:

SELECT A.ID, A.NAME, A.STORE, A.AGE, B.STOREADDRESS
FROM TableALong as A, TableBLong as B
ON A.NAME = B.NAME and A.STORE = B.STORE
Run Code Online (Sandbox Code Playgroud)

这在oracle中是无效的。让它在 oracle 中运行的正确查询是什么?我认为这是我想要的左连接?(连接后,TableALong 中的每一项都会有多行。)

Ale*_*sej 5

固定查询:

SELECT A.ID,
       A.NAME,
       A.STORE,
       B.STOREADDRESS as yourAlias /* AS is ok for column aliases ... */
FROM TableALong A                  /* ... but not for table aliases */
     LEFT OUTER JOIN TableBLong  B /* JOIN syntax */
       ON (A.NAME = B.NAME and A.STORE = B.STORE)
WHERE ...
Run Code Online (Sandbox Code Playgroud)

而不是LEFT OUTER你可以有INNER, FULL OUTER, ...; 请参阅此处了解更多信息。

  • 值得指出的是,问题出在“FROM”子句中的“as”。Oracle 不支持这一点。而且,“NATURAL JOIN”是一个即将发生的错误,所以不要使用它。 (2认同)