大家好,我现在有一个表:ID      NAME
1       A,B,C
2       B
3       B,C
现在我想查出所有包含A或C的记录,也就是第一条和第三条记录.
请问有什么好办法可以查出呢?(查询A或C只是个例子,实际上会有很多个的情况,比如:A、B、F、G...)

解决方案 »

  1.   


    with tmp as
    (
    select 1 id, 'A,B,C' name from dual
    union all
    select 2 id, 'B' name from dual
    union all
    select 3 id, 'B,C' name from dual
    )
    select id, name
    from tmp
    where regexp_like(name, '[A|C]');        ID NAME   
    ---------- -------
             1 A,B,C  
             3 B,C    
      

  2.   

    where regexp_like(name, '(^|,)[AC](,|$)');
      

  3.   

    select * from tt where regexp_like(name,'[A|C]');
      

  4.   

    原来那个有点错误,用下面这个吧with tmp as
    (
    select 1 id, 'A,B,C' name from dual
    union all
    select 2 id, '|' name from dual
    union all
    select 3 id, 'B,C' name from dual
    )
    select id, name
    from tmp
    where regexp_like(name, '[AC]')正则表达式[A|C]应该是指A,|,C其中一个;[AC]指A或C
      

  5.   

    再发个不分大小写的with tmp as
    (
    select 1 id, 'A,B,C' name from dual
    union all
    select 2 id, 'B' name from dual
    union all
    select 3 id, 'B,C' name from dual
    union all
    select 4 id, 'a,c' name from dual
    )
    select id, name
    from tmp
    where regexp_like(name, '[AC]', 'i');        ID NAME   
    ---------- -------
             1 A,B,C  
             3 B,C    
             4 a,c 
      

  6.   

    除了楼上的方法外,还有:
    with tmp as
    (
    select 1 id, 'A,B,C' name from dual
    union all
    select 2 id, 'B' name from dual
    union all
    select 3 id, 'B,C' name from dual
    union all
    select 4 id, 'a,c' name from dual
    )
    select id, name
    from tmp
    where instr(name,'C',1) > 0;