Search code examples
oracle-databaseoracle12c

OR in Select Oracle


select * from table1 t1 where t1.column1 = 'someValue' and ((t1.column2 =1) OR  (sysdate < select t1.DateColumn2 + t2.DateColumn2/1440
      from
    table2 t2 where t1.column3 = t2.column3));

if t1.column2 =1 evaluates to false, I want to check another condition if time t1.DateColumn2 + t2.DateColumn2 is < sysdate . Oracle is throwing syntax error near or condition. Cant sysdate be used directly like that? Not sure where I am going wrong. Thanks


Solution

  • If I am guessing your intention correctly, you want an exists clause

    select * 
      from table1 t1 
     where t1.column1 = 'someValue' 
       and (   (t1.column2 =1) 
            OR exists( select 1
                         from table2 t2
                        where t2.column3 = t1.column3
                          and sysdate < t1.DateColumn2 + t2.DateColumn2/1440 ));
    

    Or just join the two tables in the outer query assuming there is at most 1 row in t2 per t1 row (if there is exactly 1 row you should do an inner join rather than a left outer join)

    select t1.*
      from table1 t1
           left outer join table2 t2
             on( t1.column3 = t2.column3 )
     where t1.column2 = 1
        or sysdate < t1.DateColumn2 + t2.DateColumn2/1440;