ORA-00918:执行查询时列不明确定义的错误。

时间:2012-11-30 12:26:33

标签: sql oracle sqlplus

以下是查询:

select STOCKS.STOCKNO, STOCKS.DESCRIPTION, ORDERITEMS.QTYORD
from STOCKS
left join ORDERITEMS
on STOCKS.STOCKNO = ORDERITEMS.STOCKNO 
WHERE STOCKNO !=(Select STOCKNO from ORDERITEMS where ORDERNO='1' or ORDERNO='2')
ORDER BY STOCKNO
/

这是错误:

ORA-00918: column ambiguously defined for every stock item.. 

我已经完成了连接,但是对于第一个查询,但第二个查询是

WHERE STOCKNO !=(Select STOCKNO from ORDERITEMS where ORDERNO='1' or ORDERNO='2')

问题是显示包括股票在内的股票,描述和订购数量 到目前为止,他们还没有下订单。

3 个答案:

答案 0 :(得分:3)

哪个库存号?

WHERE STOCKNO 

STOCKS.STOCKNOORDERITEMS.STOCKNO

答案 1 :(得分:2)

尝试使用如下的别名,并将!=替换为not in

select S.STOCKNO, S.DESCRIPTION, O.QTYORD
from STOCKS S
left join ORDERITEMS O on S.STOCKNO = O.STOCKNO 
WHERE S.STOCKNO not in (select O2.STOCKNO  
                        from ORDERITEMS O2 
                        where O2.ORDERNO='1' or O2. ORDERNO='2')
ORDER BY S.STOCKNO
/

答案 2 :(得分:0)

确保使用别名。

select 
    s.stockno, 
    s.description, 
    o.qtyord
from stocks s
left join orderitems o
    on s.stockno = o.stockno 
where s.stockno not in ( select stockno from orderitems where orderno in ( '1', '2' ) )
order by s.stockno
;

特别是,您需要使用where s.stockno not in <subquery>