外连接操作符错误

时间:2016-01-20 10:30:40

标签: sql oracle

之前我只是在一个条件下过滤我的查询。即 project_id 但现在我又添加了两个过滤条件。他们是

  1. 日期IN

  2. 车辆编号

  3. 我尝试使用以下查询

    SELECT DISTINCT sv.mkey, vehicle_no,
                   TO_CHAR (date_in, 'dd/MM/yyyy')
                || ' & '
                || time_in vehicleindate_time,
                   TO_CHAR (date_out, 'dd/MM/yyyy')
                || ' & '
                || time_out vehicleoutdate_time,
                gate_no_in || ' & ' || gate_no_out ingate_outgateno,
                gd.good_type goods_type, net_weight netweight,
                   TO_CHAR (challan_date, 'dd/MM/yyyy')
                || ' & '
                || challan_no challandate_no,
                remark_in remarkin, NULL receipt_no, date_in
           FROM xxcus.xxgid_supinv sv,
                xxcus.xx_supinv_goodtype gd,
                xxcus.xxacl_xxgid_user_mst ms
          WHERE gd.good_type_code(+) = sv.good_type AND sv.project_id = 1469
             OR TO_CHAR (date_in, 'dd/MM/yyyy') = '09/01/2015'
             OR vehicle_no = '79'
       ORDER BY date_in DESC, vehicle_no
    

    但是出现错误

      

    ORA-01719:OR或IN

    的操作数中不允许使用外连接运算符(+)

    我不知道这里出了什么问题。请建议

2 个答案:

答案 0 :(得分:1)

您需要使用明确的JOIN,并且还需要定义将xxcus.xxacl_xxgid_user_mst ms加入到查询的其余部分的内容。

SELECT DISTINCT sv.mkey, vehicle_no,
                TO_CHAR (date_in, 'dd/MM/yyyy') || ' & ' || time_in vehicleindate_time,
                TO_CHAR (date_out, 'dd/MM/yyyy') || ' & ' || time_out vehicleoutdate_time,
                gate_no_in || ' & ' || gate_no_out ingate_outgateno,
                gd.good_type goods_type, net_weight netweight,
                TO_CHAR (challan_date, 'dd/MM/yyyy') || ' & ' || challan_no challandate_no,
                remark_in remarkin, NULL receipt_no, date_in
FROM xxcus.xxgid_supinv sv
RIGHT OUTER JOIN xxcus.xx_supinv_goodtype gd ON sv.good_type = gd.good_type_code
XXXX JOIN xxcus.xxacl_xxgid_user_mst ms ON XX.XXXXX ON ms.XXXXX
WHERE sv.project_id = 1469
OR TO_CHAR (date_in, 'dd/MM/yyyy') = '09/01/2015'
OR vehicle_no = '79'
ORDER BY date_in DESC, vehicle_no

答案 1 :(得分:0)

您正在使用(+)语法,它不允许使用OR条件;你可以使用显式OUTER JOIN重写你的查询;例如:

SQL> create table table_id ( id number);

Table created.

SQL> create table table_desc ( id number, description varchar2(255));

Table created.

SQL> begin
  2  insert into table_id values (1);
  3  insert into table_id values (2);
  4  insert into table_id values (3);
  5  insert into table_desc values (1, 'ONE');
  6  insert into table_desc values (3, 'THREE');
  7  insert into table_desc values (3, 'Three');
  8  end;
  9  /

PL/SQL procedure successfully completed.

错误的方式:

SQL> select *
  2  from table_id id, table_desc des
  3  where id.id = des.id(+)
  4    and des.description = 'Three' OR des.description = 'THREE';
where id.id = des.id(+)
            *
ERROR at line 3:
ORA-01719: outer join operator (+) not allowed in operand of OR or IN

正确的方式:

SQL> select id.id, des.description
  2  from table_id id
  3       LEFT OUTER JOIN table_desc des
  4        ON id.id = des.id
  5  where des.description = 'Three' OR des.description = 'THREE';

        ID DESCRIPTION
---------- -------------------
         3 Three
         3 THREE

SQL>