日期:2014-05-16  浏览次数:20708 次

请教一个select语句的条件写法

┏━━━━━┯━━━━┯━━━━━┓
┃id │det │flag ┃
┠─────┼────┼─────┨
┃ 10011│ 1│null ┃
┠─────┼────┼─────┨
┃ 10021│null │null ┃
┠─────┼────┼─────┨
┃1003A │ 2│A ┃
┠─────┼────┼─────┨
┃1003B │ 3│null ┃
┠─────┼────┼─────┨
┃1004A │null │A ┃
┗━━━━━┷━━━━┷━━━━━┛

这样一个表,选出det不为空的,如果id末尾为A或B并且flag不为空的也选上,结果如下:

┏━━━━━┯━━━━┯━━━━━┓
┃id │det │flag ┃
┠─────┼────┼─────┨
┃ 10011│ 1│null ┃
┠─────┼────┼─────┨
┃1003A │ 2│A ┃
┗━━━━━┷━━━━┷━━━━━┛


------解决方案--------------------
where det is not null or (substr(id,-1) in ('A','B') and flag is not null)
------解决方案--------------------
SQL code

where det is not null and((substr(id,-1) in ('A','B') and flag is not null )or substr(id,-1) not in ('A','B'))

------解决方案--------------------
SQL code
where det is not null 
and((substr(id,-1) in ('A','B') and flag is not null )or substr(id,-1) not in ('A','B'))