sql当field2 ='something'然后field3之类的

时间:2013-06-05 16:13:18

标签: sql oracle oracle11g

我的查询是

Select 
       NO,
       Timestamp,
       Event_type,
       Comments
From   TableA
Where NO = '12345';

但对于Event_type,我只想选择何时event_type in ('ABC_XYZ','Comment_Change'

并且Comments字段必须与Cause:%event_type = 'Comment_change'

时相同

我怎样才能实现这个条件?

4 个答案:

答案 0 :(得分:1)

Select 
       NO,
       Timestamp,
       Event_type,
       Comments
From   TableA
Where NO = '12345'
and event_type = 'Comment_Change' 
And Comments like 'Cause:%';

这似乎是你要求的。

答案 1 :(得分:1)

在mysql中:

Select
    no,
    Timestamp,
    if(event_type='comment_change',event_type,null) as event_type,
    Concat('Cause: ', comments) as comments
from TableA
where no='12345';

答案 2 :(得分:1)

带有案例构造

select case when comments like 'Cause:%'  
and event_type in ('ABC_XYZ','Comment_Change') then event_type 
else 'something else' event_type
from tableA
where no = '12345'

答案 3 :(得分:1)

尝试:

Select 
       NO,
       Timestamp,
       Event_type,
       Comments
From   TableA
Where event_type = 'ABC_XYZ' or
      (event_type =  'Comment_Change' and Commentsfield like 'Cause:%')