内部联接别名导致问题

时间:2014-03-29 09:35:49

标签: sql oracle alias

我有4张桌子。

create table LOCATION(
loccode Number(2) NOT NULL,
locname Varchar2 (30),
state Varchar2 (10),
population Number(7),
travelcode Number(1),
CONSTRAINT PK_LOCATION PRIMARY KEY (loccode)
);

create table DEPOT(
depid Number(4) NOT NULL,
audit_year Number(4),
address Varchar2 (30),
mgrname Varchar2 (30),
capacity Number(7),
capacity_used Number(7),
mgrgender Varchar2(10),
loccode Number(2) NOT NULL,
CONSTRAINT PK_DEPOT PRIMARY KEY (depid),
CONSTRAINT FK_LOCATION FOREIGN KEY (loccode) REFERENCES LOCATION

create table MANUFACTURER(
manid Number (2) NOT NULL,
manname Varchar2(30),
loccode Number(2) NOT NULL,
CONSTRAINT PK_MANUFACTURER PRIMARY KEY (manid),
CONSTRAINT FK_LOC FOREIGN KEY (loccode) REFERENCES LOCATION
);
create table STOCKITEM(
stkid Number(2) NOT NULL,
stkname Varchar2(30),
sellingprice Number(6,2),
purchaseprice Number(6,2),
depid Number(4) NOT NULL,
manid Number(2) NOT NULL,
CONSTRAINT PK_STOCKITEM PRIMARY KEY (stkid),
CONSTRAINT FK_DEP FOREIGN KEY (depid) REFERENCES DEPOT,
CONSTRAINT FK_MANUFACTURER FOREIGN KEY (manid) REFERENCES MANUFACTURER 
);

我正在尝试生成一个查询,让我查看库存ID,库存名称,制造商位置和部门位置。

这需要4个内连接子句。

1将stockitem链接到仓库,一个将stockitem链接到制造商,另外两个将位置链接到仓库和制造商。

我现在正在使用它;

select  
    stockitem.stkid as "STOCK ID",
    stockitem.stkname as "STOCK NAME", 
    manloc.locname as "MANUFACTURER LOCATION", 
    deploc.locname as "DEPOT LOCATION"  
from stockitem
inner join depot on stockitem.depid = depot.depid
inner join manufacturer on stockitem.manid = manufacturer.manid
inner join location  on location.locid = deploc.locid as deploc
inner join location  on location.locid = manloc.locid as manloc
order by stockitem.stkid asc;

然而它正在吐痰,字符串没有正确终止。我在这里错过了什么吗?

如果我把别名放在' on'之前。 (deploc和manloc),然后我得到一个错误,说它错过了一个预期的操作员......即......' on'。

我在哪里错了?

1 个答案:

答案 0 :(得分:2)

位置表别名应该在表名之后,当别名表(这是Oracle怪癖)时应该没有 AS

select  
stockitem.stkid as "STOCK ID",
stockitem.stkname as "STOCK NAME", 
manloc.locname as "MANUFACTURER LOCATION", 
deploc.locname as "DEPOT LOCATION"  
from stockitem
inner join depot on stockitem.depid = depot.depid 
inner join manufacturer on stockitem.manid = manufacturer.manid 
inner join location deploc on depot.loccode = deploc.loccode 
inner join location manloc on manufacturer.loccode = manloc.loccode 
order by stockitem.stkid asc;

当别名列时,您实际上不需要AS,但它使阅读更容易。

请注意位置内连接中的正确列引用