码迷,mamicode.com
首页 > 数据库 > 详细

mysql 行转列(存储过程)

时间:2016-07-07 00:43:51      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

直接sql(自己抽离代码,无法和order by rand() 复用):

select shit from( select 要累加的字段, @tt:=CONCAT(@tt,,,a.要累加的字段) as shit from 某表 a,(select @tt:=‘‘) b ) c ORDER BY LENGTH(c.shit)
 desc limit 0,1

 

 

存储过程版本:

BEGIN
    DECLARE Is_End INT DEFAULT 0;
    DECLARE _str VARCHAR(50) DEFAULT ‘‘;
    DECLARE _id INT;
    DECLARE _answer VARCHAR(50);
    DECLARE cur CURSOR FOR SELECT id,answer FROM question_library where id > 100 ORDER BY RAND() LIMIT 10;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET Is_End = 1;
    OPEN cur;
    FETCH cur INTO _id,_answer;
    WHILE Is_End != 1 DO
        SELECT CONCAT(_id,,,_str) INTO _str;
        FETCH cur INTO _id,_answer;
    END WHILE;
    CLOSE cur;
    SELECT left(_str,LENGTH(_str)-1) AS question;  #去掉最后一个字符串“,”
END

 

mysql 行转列(存储过程)

标签:

原文地址:http://www.cnblogs.com/CyLee/p/5648407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!