如果在select子句中

时间:2015-02-10 13:53:23

标签: sql oracle

假设我有以下SQL select子句:

SELECT * FROM some_table_1 t1
  join some_table t2 on t2.some_id =  t1.id and
  t2.school_id = 56 and
  t1.is_valid = 1 and 
  t2.status in (15, 16, 17, 18);

但我还要检查,如果t2.status = 18,那么我还要检查是否t2.date < 01.01.2015

如何将此条件添加到此select

3 个答案:

答案 0 :(得分:1)

听起来你只想在日期时加入状态为18的行(这是一个列的错误名称,因为它是一个Oracle保留字。我也将假设它是DATE数据类型)小于2015年1月1日。如果是这样,那么以下应该做的伎俩:

SELECT *
FROM   some_table_1 t1
       join some_table t2 on t2.some_id =  t1.id 
                             and t2.school_id = 56
                             and t1.is_valid = 1
                             and (t2.status in (15, 16, 17)
                                  or (t2.status = 18 and t2.date < to_date('01.01.2015', 'dd.mm.yyyy.')));

答案 1 :(得分:0)

只需添加or检查'不是18',或匹配您的第二个条件:

SELECT *
FROM   some_table_1 t1
join   some_table t2
on     t2.some_id = t1.id
and    t2.school_id = 56
and    t1.is_valid = 1
and    t2.status in (15, 16, 17, 18)
/* added part */
where  ( t2.status != 18
         or t2.date < to_date('01.01.2015', 'dd.MM.yyyy')
       )

答案 2 :(得分:0)

您可以添加:

where t2.status <> 18 or t2.date < date '2015-01-01'