Oracle子查询错误

时间:2016-06-24 09:16:39

标签: sql oracle

select SELLS.BEER, SELLS.PRICE, SELLS.BAR 
from   FREQUENTS 
where  FREQUENTS.DRINKER = 'Sally' 
  and  FREQUENTS.BAR IN (select SELLS.BAR from SELLS where PRICE > 5);

以下是错误消息:

ORA-00904: "SELLS"."BAR": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Error at Line: 43 Column: 33

我已经检查了表格中的所有列,但它们看起来对我来说却无法找到。

有人能告诉我哪里出错了吗?我试图在不使用JOIN的情况下解决它。

2 个答案:

答案 0 :(得分:1)

这可能有效:

select s.BEER, s.PRICE, s.BAR 
from FREQUENTS, SELLS s
where FREQUENTS.DRINKER = 'Sally' AND FREQUENTS.BAR IN (select SELLS.BAR from SELLS where PRICE > 5);

如果我得到你的逻辑,试试这个:

select s.BEER, s.PRICE, s.BAR 
from FREQUENTS, SELLS s
where FREQUENTS.DRINKER = 'Sally' AND FREQUENTS.BAR IN (select SELLS.BAR from SELLS where s.PRICE > 5);

答案 1 :(得分:1)

卖家没有加入主要查询,所以您选择的内容并不有效。

SELECT s.BEER, s.PRICE, s.BAR 
FROM FREQUENTS f
INNER JOIN SELLS s ON f.BAR = s.BAR
WHERE f.DRINKER = 'Sally' 
AND f.BAR IN (SELECT sa.BAR 
              FROM SELLS sa 
              WHERE sa.PRICE > 5);

可能更简单:

SELECT s.BEER, s.PRICE, s.BAR 
FROM FREQUENTS f
INNER JOIN SELLS s ON f.BAR = s.BAR
WHERE f.DRINKER = 'Sally' 
AND s.PRICE > 5;

没有加入:

SELECT s.BEER, s.PRICE, s.BAR 
FROM SELLS s
WHERE s.BAR = (SELECT f.BAR
               FROM FREQUENTS f
               WHERE f.DRINKER = 'Sally')
AND s.PRICE > 5;