下面是一张数据表 A
ID    name    other
1     张三    
2     李思    4710,998
我现在想取出不含有4710的数据,当然这个值是按参数传入的,参数为userId,SQL如下select * from A where instr(other,'4710')=0;我原本以为会出现第一条记录,结果,结果集为空!!!!试了半天没弄出来,这个应该怎么写哦

解决方案 »

  1.   

    select * from A where instr(other,'4710')=0 or other is null
      

  2.   

    SQL> WITH temp AS
      2     (SELECT 1 ID,'张三' NAME,'' other FROM dual
      3      UNION ALL
      4      SELECT 1 ID,'李思' NAME,'4710,998' other FROM dual)
      5  select * from temp where instr(other,'4710')=0 OR other IS NULL;        ID NAME OTHER
    ---------- ---- --------
             1 张三 SQL> select instr(null,'4710') from dual;INSTR(NULL,'4710')
    ------------------SQL> 
    函数处理null,结果也为null,而null!=0
      

  3.   

    SELECT *
      FROM A
     WHERE INSTR (NVL (other, 'aaa'), '4710') = 0
      

  4.   

    null比较特殊,直接对它用操作的话不会返回你希望的结果,所以你需要用nvl函数处理掉null值
    SELECT *
      FROM A
     WHERE INSTR (NVL (other, 'aaa'), '4710') = 0