首页 > 解决方案 > 异常加入Oracle

问题描述

              SELECT a.product_name
                   , a.product_price
                   , c.restaurant_id
                   , c.restaurant_name
                   , c.phone_number
                FROM MENU a 
RIGHT EXCEPTION JOIN RESTAURANT c 
                  ON a.restaurant_id=c.restaurant_id 
            ORDER BY a.restaurant_id
                   ;

我收到一个错误

答:ORA-00905 用于指示格式错误的语句,其中 Oracle 解析器指示语句缺少关键字。

Oracle 文档在 ora-00905 错误中记录了这一点:

原因:缺少必需的关键字。... 遇到 ORA-00905 时,您必须更正语法,因为缺少关键字。

标签: sqloracleexceptionjoin

解决方案


exception join在 Oracle中没有 a 这样的东西。就问题而言,只有 DB2 支持这种语法。

如果您想要所有没有餐厅的菜单,我建议not exists

select m.*
from menu m
where not exists (select 1 from restaurant r where r.restaurant_id = m.restaurant_id)
order by m.restaurant_id

你也可以用反left join

select m.*
from menu m
left join restaurant r on r.restaurant_id = m.restaurant_id 
where r.restaurant_id is null
order by m.restaurant_id    

另一方面,如果您希望所有餐厅都没有菜单,您可以反转查询中的表格:

select r.*
from restaurant r
where not exists (select 1 from menu m where r.restaurant_id = m.restaurant_id)
order by r.restaurant_id

推荐阅读