加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 搜索优化 > 正文

mysql5中怎么在使用过程达成行转列

发布时间:2022-02-12 18:33:38 所属栏目:搜索优化 来源:互联网
导读:本篇内容主要讲解mysql5中怎么在使用过程实现行转列,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习mysql5中怎么在使用过程实现行转列吧! 简单到网上搜了一下,没有发现mysql有类似oracle 的decode函数实现行
       本篇内容主要讲解“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中怎么在使用过程实现行转列”有了更深的了解,不妨来实际操作一番吧!

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读