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

时间:2016-04-01 19:16:13

标签: sql oracle

我有以下问题。我有两张桌子要加入" 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

我想在加入之前做一些像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

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

1 个答案:

答案 0 :(得分:2)

固定查询:

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 ...

而不是LEFT OUTER您可以拥有INNERFULL OUTER,...;有关详情,请参阅here