有一列ID保存在字符串中: "111,222,333,444,555,666"
现在想查找这列ID中,哪些是错误的,也就是不存在表中的。下面这段SQL可能会帮助理解需求。
select * from table t where (889814, 889820, 889828) not in t.id

解决方案 »

  1.   

    with tb as
    (
      select '111,222,333,444,555,666' id from dual
    ),
    tb1 as( --将字符串拆分成表
    select rtrim(ltrim(substr(id, 4 * (rownum - 1), 4), ','), ',') newid
    from tb
    connect by rownum <= length(id) - length(replace(id, ',', '')) + 1
    ),
    tb2 as(
    select '111' id from dual union all
    select '333' from dual)
    select * from tb1
    where not exists(select null from tb2 where tb1.newid=tb2.id)
    --查找字符串'111,222,333,444,555,666'中不存在tb2中的那些字符串NEWID
    --------
    666
    555
    222
    444
      

  2.   


    --首先要将你字串“111,222,333,444,555,666”拆分成一个一个的,然后跟你另外的表去做判断
    with tab as(
    select '111,222,333,444,555,666'  id from dual
    )
    select substr(','||id||',',instr(','||id||',',',',1,level)+1,
              instr(','||id||',',',',1,level+1)-instr(','||id||',',',',1,level)-1) newid
    from tab
    connect by
    level <= length(','||id||',') - length(replace(','||id||',', ',', ''))-1NEWID
    --------
    111
    222
    333
    444
    555
    666