表a
id (bigint)       contentid (varchar)
1                   [{"id":"2"},{"id":"6"}]
2                   [{"id":"8"},{"id":"9"},{"id":"5"}]
3                   [{"id":"7"},{"id":"2"},{"id":"4"},{"id":"22"},{"id":"11"}]
在触发器里怎么得到contentid字段中的数字

解决方案 »

  1.   

    你想得到的是哪个数字?
    [{"id":"8"},{"id":"9"},{"id":"5"}]是第一个,还是最后一个? 第一个 SELECT SUBSTRING_INDEX(contentid, '"', 4)
      

  2.   

    往表a里插入一条就得到那一行contentid字段里的数字
      

  3.   

    那2                   [{"id":"8"},{"id":"9"},{"id":"5"}]这一行中的数字是什么? 八百九十五 ?
     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  4.   

    不好意思我没说清楚。
    这个是表a的数据。
    结构。
    我的意思是把contentid里的数字截取出来插入到表b的content字段中。每一个数字向表b中插入一条数据。
    如果要插入的数字在表b的content字段中有了那就把count字段加一。
    这是表b的数据。
    结构。
    不知道您听懂了吗?
      

  5.   

    --建立你的b表 
    drop table if exists b;
    create table b (content int);--建立你的测试表a
    drop table if exists a ;
    create table a(id bigint auto_increment primary key,contentid varchar(1000));--建立你的触发器
    delimiter $$create trigger  trinsert after insert on a
    for each row
    begin 
    declare n ,num int;
    set n=1;
    set num=char_length(new.contentid)-char_length(replace(new.contentid,',',''))+1;
    while (n<=num) do
      if n=1 then 
                        insert b  values(replace(replace(replace(replace(replace(replace(substring_index(concat(new.contentid,','),',',n),'[',''),'{',''),'}',''),':',''),'"',''),'id',''));
                      else 
             insert b values(replace(replace(replace(replace(replace(replace(replace(substring_index(concat(new.contentid,','),',',n),concat(substring_index(concat(new.contentid,','),',',n-1),','),''),']',''),'{',''),'}',''),':',''),'"',''),'id',''));
      end if ;
             set n=n+1;
            end while;   
    end;
    $$delimiter ;--测试下
    --插入记录
    insert a select null,'[{"id":"2"},{"id":"6"}]';
    --检验下结果 
    select * from b;+---------+
    | content |
    +---------+
    |       2 |
    |       6 |
    +---------+insert a select null,'[{"id":"8"},{"id":"9"},{"id":"5"}]';
    select * from b ;
    +---------+
    | content |
    +---------+
    |       2 |
    |       6 |
    |       8 |
    |       9 |
    |       5 |
    +---------+
      

  6.   

    里面截取字符串的地方能不能这样写
       while (n<=num)    do
              if n=1 then 
        set @c=replace(replace(replace(replace(replace(replace(substring_index(concat(new.contentid,','),',',n),'[',''),'{',''),'}',''),':',''),'"',''),'id','');
                        insert b  values(@c);
                      
    else 
         set @b=replace(replace(replace(replace(replace(replace(replace(substring_index(concat(new.contentid,','),',',n),concat(substring_index(concat(new.contentid,','),',',n-1),','),''),']',''),'{',''),'}',''),':',''),'"',''),'id','');
                 insert b values(@b);
              end if ;
                set n=n+1;
            end while;