假设有这样一个表
tablename
列定义如下:(其实不止这些列)
 a varchar(),b varchar(),c dateselect t.* from tablename 假设result 为a , ddGT ,2008-10-01
b , dd ,2008-10-01
c , ccGT ,2008-10-01
c , cc ,2008-10-01
c , eeGT ,2008-10-01.......
%%GT和%% 如过同时出现只取%%
但是如果只有%%GT 则才显示%%GT的记录
比如:
当有b字段有ddGT,dd的记录时候只显示b字段中为dd的记录即可
但是如果b字段只有ddGT则才显示ddGT,即没有dd是才选择喊有ddGT的字段比如上面的结果集应该为b , dd ,2008-10-01
c , cc ,2008-10-01
c , eeGT ,2008-10-01
........

解决方案 »

  1.   

    这个GT前面的dd,cc是固定就这两个值吗?如果固定就很容易。否则可用下面这个通用方法:
    OPER@TL>select * from test;A          B
    ---------- ----------
    a          ddGT
    b          dd
    c          ccGT
    c          cc
    c          eeGTOPER@TL>select * from test
      2  where b in(select substr(b,1,instr(b,'GT')-1) from test)
      3  union all
      4  select * from test
      5  where b not in(select nvl(substr(b,1,instr(b,'GT')-1),'X') from test)
      6  and nvl(substr(b,1,instr(b,'GT')-1),'X') not in(select b from test);A          B
    ---------- ----------
    b          dd
    c          cc
    c          eeGTOPER@TL>
      

  2.   

    为什么加上这句and nvl(substr(b,1,instr(b,'GT')-1),'X') not in(select b from test);
    这个GT前面的dd,cc不是固定只有这两个值
    数据很多 用in的 话回不会 很慢呢  数据上10w
      

  3.   

    试试这个
    select * from test where b not like '%GT'
    union
    select * from test where b like '%GT' and b not in
      ( select b||'GT' from test where b not like '%GT')