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

发布时间:2021-07-27 11:43 来源:亿速云 阅读:0 作者:chen 栏目: Mysql 欢迎投稿:712375056

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

免责声明:本站发布的内容(图片、视频和文字)以原创、来自本网站内容采集于网络互联网转载等其它媒体和分享为主,内容观点不代表本网站立场,如侵犯了原作者的版权,请告知一经查实,将立刻删除涉嫌侵权内容,联系我们QQ:712375056,同时欢迎投稿传递力量。