现在有两张表:
1.  user_base_info
    表结构  id(varchar)  userId(varchar)  title(varchar)  content(varchar)
            1                  159            性别           男
            2                  159            年龄           19     
            3                  159            城市           北京
            4                  357            性别           女
            5                  357            年龄           22
            6                  357            城市           上海
            ...2.  user_search
    表结构  id    userId(varchar)  age(varchar)  gender(varchar)  city(varchar)
            1        159            null           null             null
            2        357            null           null             null 问题是 在Mysql中使用存储过程或游标 如何将user_base_info表中的数据更新到user_search表中 并转换成下面的格式:
            id    userId(varchar)  age(varchar)  gender(varchar)  city(varchar)
            1        159            19             男             北京
            2        357            22             女             上海

解决方案 »

  1.   

    不需要存储过程,直接用SQL语句应该就可以了。update user_search a 
    inner join user_base_info b1 on a.userId=b1.userId and b1.title='年龄'
    inner join user_base_info b2 on a.userId=b2.userId and b2.title='性别'
    inner join user_base_info b3 on a.userId=b3.userId and b3.title='城市'
    set a.age=b1.content,a.gender=b2.content,a.city=b3.content
      

  2.   

    user_base_info表中一个人有好多条数据,而且user_search也有足够的字段。
    所以我不想用join的方式取得并插入,这样有点耗时。
      

  3.   

    UPDATE user_search a1 INNER JOIN user_base_info b ON a1.userid=b.userid AND b.title='年龄'
    INNER JOIN user_base_info b1 ON a1.userid=b1.userid AND b1.title='性别'
    INNER JOIN user_base_info b2 ON a1.userid=b2.userid AND b2.title='城市'
    SET a1.age=b.content,
    a1.gender=b1.content,
    a1.city=b2.content;
      

  4.   

    那就在存储过程中一个一个字段逐步替换update user_search a inner join user_base_info b1 on a.userId=b1.userId and b1.title='年龄'
    set a.age=b1.content;update user_search a inner join user_base_info b2 on a.userId=b2.userId and b2.title='性别'
    set a.gender=b2.content;update user_search a inner join user_base_info b3 on a.userId=b3.userId and b3.title='城市'
    set a.city=b3.content;...
      

  5.   

    不是吧,你的索引情况,在userId title上建立复合索引试试
      

  6.   

    CREATE PROCEDURE `atr_user_query_b`()
    begindeclare userId varchar(50);
    declare bitc varchar(50);
    declare bic varchar(50);declare l_userId cursor for select distinct User_Id from atr_user_search; /**userId**/
    declare l_baseInfo_t cursor for select base_info_title from atr_user_base_info where base_info_userId=userId; /**base_info_title**/
    declare l_baseInfo_c cursor for select base_info_content from atr_user_base_info where base_info_userId=userId and base_info_title=bitc; /**base_info_content**/open l_userId;
    fetch l_userId into userId;
    begin
    open l_baseInfo_t;
    fetch l_baseInfo_t into bitc;
    begin
    open l_baseInfo_c;
    fetch l_baseInfo_c into bic;
    begin
    if (bitc = '现居住地') then
    update atr_user_search set User_Search_13=bic where User_Id=userId;
    end if;
    end;
    close l_baseInfo_c;
    end;
    close l_baseInfo_t;
    end;
    close l_userId;
    end这是我做的一个测试 貌似没成功 5555
      

  7.   

    有合适的索引,SQL语句一般比游标快,你的索引情况?