温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mysql5中怎么在使用过程实现行转列

发布时间:2021-07-12 09:22:07 来源:亿速云 阅读:116 作者:chen 栏目:MySQL数据库

本篇内容主要讲解“mysql5中怎么在使用过程实现行转列”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mysql5中怎么在使用过程实现行转列”吧!

简单到网上搜了一下,没有发现mysql有类似oracle 的decode函数实现行转列,有不少用过程实现动态行转列,试了一下,不适合我用,参考mysql5的帮助,写个个过程,记录备忘。其中用到了游标,关于游标的使用文档里给的例子也不错,反正我就只会那么用。。。

DELIMITER $$

DROP PROCEDURE IF EXISTS `hrb_yd`.`proc_hrb_yd_stat`$$

CREATE DEFINER=`root`@`192.168.96.1` PROCEDURE `proc_hrb_yd_stat`(IN $G_id INTEGER(10))
BEGIN
declare v_name VARCHAR(255);
declare  v  INT DEFAULT 0;
declare  v1  INT DEFAULT 0;
declare  v2  INT DEFAULT 0;
declare  v3  INT DEFAULT 0;
declare  v4  INT DEFAULT 0;
declare  v5  INT DEFAULT 0;
declare  v6  INT DEFAULT 0;
declare a int ;
declare b varchar(255);
DECLARE done INT DEFAULT 0;
declare cur1 cursor for select id,name from  hrb_yd_admin  where g_id=$G_id ;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

select count(id) into v from hrb_yd_admin where g_id=$G_id;
open cur1;
repeat
fetch cur1 into  a,b;
if  not done then
select count(id) into v1 from hrb_yd_visit_table  where  user_id=a  and  visit_way='实地走访' ; /*实地走访次数*/
select count(id) into v2 from hrb_yd_visit_table  where  user_id=a  and visit_way='电话'  ;    /*--电话拜访方式次数*/
select count(id) into v3 from hrb_yd_visit_table  where  user_id=a  and visit_way='E—mail'  ;  /*--emianl拜访方式次数*/
select count(id) into v4 from hrb_yd_visit_table  where  user_id=a  and visit_way ='' ;        /*--其他拜访方式次数*/
select count(id) into v5 from hrb_yd_visit_table  where  user_id=a  and visit_un_purpose='是';  /*--推荐成功数*/
select count(id) into v6 from hrb_yd_visit_table  where  user_id=a  and visit_un_purpose='否'; /*--未推荐成功数*/
select b,v1,v2,v3,v4,v5,v6;
end if;
until done end repeat;
close cur1;

END$$

DELIMITER ;

到此,相信大家对“mysql5中怎么在使用过程实现行转列”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI