id    IMAGEID
1 5,24
2 192
3
4
5
6 5,24,121,3,34
7
8
9
10
11 其中IMAGEID没写的表明该值为空。我想统计IMAGEID中值的个数,有逗号要拆开统计。比如上面的结果应该是5、24、192、5、24、121、3、34个数的总合。(应该是8)
这样的sql语句该怎样写呀?当然表中的记录不只是11条,会有很多,不可能一个一个地数呀。

解决方案 »

  1.   

    一条sql估计是不行了,需要些个存储过程
      

  2.   

    --原来这样可以  :)--测试数据
    create table ttt( id int, IMAGEID varchar2(1000));
    insert into ttt values(1,'5,24');
    insert into ttt values(2,'192');
    insert into ttt(id) values(3);
    insert into ttt(id) values(4);
    insert into ttt values(5,'5,24,121,3,34');
    --执行查询
    select sum(length(IMAGEID)-length(replace(IMAGEID,',','')) +1) sum
    from ttt
    where imageid is not null--输出结果
    8
      

  3.   

    --这回可以了吧create or replace procedure sp_GetCount( acount out int )
    is
    begin
      select sum(length(IMAGEID)-length(replace(IMAGEID,',','')) +1) into acount
      from ttt
      where imageid is not null
    end sp_GetCount;