分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
前段时间做一个项目有数据格式如下
例如 1,2,3 把1、2、3的名称查询出来拼接一个字符串返回来,用的数据库是mysql, mysql的 function代码如下
- 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 ;