共計 1575 個字符,預計需要花費 4 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
本篇內容主要講解“mysql5 中怎么在使用過程實現行轉列”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓丸趣 TV 小編來帶大家學習“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 中怎么在使用過程實現行轉列”有了更深的了解,不妨來實際操作一番吧!這里是丸趣 TV 網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
向 AI 問一下細節