Mysql拆分字符串查询

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

前段时间做一个项目有数据格式如下

   例如   1,2,3 把1、2、3的名称查询出来拼接一个字符串返回来,用的数据库是mysql, mysql的 function代码如下

 

  1. DELIMITER $$  
  2.   
  3. DROP FUNCTION IF EXISTS `tms1`.`GetClassName` $$  
  4. CREATE FUNCTION `GetClassName`(f_string VARCHAR(15000)) RETURNS varchar(15000)  
  5. BEGIN  
  6.   
  7.    /* 判断字符串包含,的第一个位置*/  
  8.    DECLARE THE_CNT INT(15) DEFAULT 1;  
  9.   
  10.    /* 班级编号*/  
  11.    declare classId varchar(20default '';  
  12.      
  13.    /* 返回的班级名称*/  
  14.    DECLARE result varchar(15000) DEFAULT null;  
  15.   
  16.    /* 班级名称*/  
  17.    DECLARE className varchar(50) DEFAULT '';  
  18.   
  19.    /* 字符串包含,的第一个位置*/  
  20.    set THE_CNT =  LOCATE(',',f_string);  
  21.   
  22.    /* 判断字符串包含,的第一个位置是否存在*/  
  23.    while (THE_CNT >= 0do  
  24.   
  25.    /* ,位置不存在的场合*/  
  26.    if THE_CNT = 0 then  
  27.         
  28.       /* 班级编号的设置*/  
  29.       set classId = f_string;  
  30.   
  31.    else  
  32.   
  33.      /* 字符串中获得班级编号*/  
  34.      set classId = SUBSTRING_INDEX(SUBSTRING_INDEX(f_string, ','1), ',', -1);  
  35.   
  36.    end if ;  
  37.   
  38.     /* 根据班级编号获得班级名称*/  
  39.     select (select name from class where id = classId) into className;  
  40.         
  41.       /* 返回班级编号的字符串为空的场合*/  
  42.       if result is null then  
  43.   
  44.          /* 根据编号没有查询到班级名称的场合*/  
  45.          if className is null then  
  46.   
  47.             /* 设置班级名称为空*/  
  48.             set className = '  ';  
  49.   
  50.          end if;  
  51.               
  52.             /* 班级名称追加到字符串*/  
  53.             set result = className;  
  54.   
  55.       else  
  56.   
  57.          /* 根据编号没有查询到班级名称的场合*/  
  58.          if className is null then  
  59.   
  60.             /* 设置班级名称为空*/  
  61.             set className = '  ';  
  62.   
  63.          end if;  
  64.   
  65.             /* 班级名称追加到字符串*/  
  66.             set result = CONCAT(result,',',className);  
  67.   
  68.       end if;  
  69.       
  70.     /* ,位置不存在的场合*/  
  71.     if THE_CNT = 0 then  
  72.        
  73.      /* 返回结果集*/  
  74.      return result;  
  75.   
  76.     end if;  
  77.   
  78.     /* 截取传入的字符串*/  
  79.     set f_string = right(f_string,length(f_string) - THE_CNT);  
  80.   
  81.     /* 字符串包含,的第一个位置*/  
  82.     set THE_CNT =  LOCATE(',',f_string);  
  83.   
  84.    /* 结束遍历*/  
  85.    end while;  
  86.   
  87.    /* 返回结果集*/  
  88.    return result;  
  89.   
  90.    END $$  
  91.   
  92. DELIMITER ;  
DELIMITER $$DROP FUNCTION IF EXISTS `tms1`.`GetClassName` $$CREATE FUNCTION `GetClassName`(f_string VARCHAR(15000)) RETURNS varchar(15000)BEGIN   /* 判断字符串包含,的第一个位置*/   DECLARE THE_CNT INT(15) DEFAULT 1;   /* 班级编号*/   declare classId varchar(20) default '';      /* 返回的班级名称*/   DECLARE result varchar(15000) DEFAULT null;   /* 班级名称*/   DECLARE className varchar(50) DEFAULT '';   /* 字符串包含,的第一个位置*/   set THE_CNT =  LOCATE(',',f_string);   /* 判断字符串包含,的第一个位置是否存在*/   while (THE_CNT >= 0) do   /* ,位置不存在的场合*/   if THE_CNT = 0 then            /* 班级编号的设置*/      set classId = f_string;   else     /* 字符串中获得班级编号*/     set classId = SUBSTRING_INDEX(SUBSTRING_INDEX(f_string, ',', 1), ',', -1);   end if ;    /* 根据班级编号获得班级名称*/    select (select name from class where id = classId) into className;            /* 返回班级编号的字符串为空的场合*/      if result is null then         /* 根据编号没有查询到班级名称的场合*/         if className is null then            /* 设置班级名称为空*/            set className = '  ';         end if;                        /* 班级名称追加到字符串*/            set result = className;      else         /* 根据编号没有查询到班级名称的场合*/         if className is null then            /* 设置班级名称为空*/            set className = '  ';         end if;            /* 班级名称追加到字符串*/            set result = CONCAT(result,',',className);      end if;        /* ,位置不存在的场合*/    if THE_CNT = 0 then          /* 返回结果集*/     return result;    end if;    /* 截取传入的字符串*/    set f_string = right(f_string,length(f_string) - THE_CNT);    /* 字符串包含,的第一个位置*/    set THE_CNT =  LOCATE(',',f_string);   /* 结束遍历*/   end while;   /* 返回结果集*/   return result;   END $$DELIMITER ;

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/hfrujhv/article/details/84094977
今日推荐