您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

mysql怎么存数组

2024/3/12 6:55:04发布17次查看
mysql存储数组的方法:先将传入的数组字符串截取成多个字符;然后传入到临时表中;最后使用游标或者直接关联表过滤数据,即可达到mysql存数组的目的了。
mysql存储过程功能弱问题一直是大家关注的问题,今天讲一下mysql存储过程无法传递数组类型参数的解决方案。
推荐课程:mysql教程。
在很多的情况下,在编写存储过程中往往会用到数组,但是mysql中存储过程传入参数并没有可以直接传入数组的方法。在这种情况下我们只能退而求之或者说换个方式以字符串形式传入参数,然后在过程体中把字符串再转成数组?
不过很遗憾告诉你,mysql并没有直接提供把字符串转数组的函数。现在你是不是有种想打人的感觉呢?不过,不用慌,此路不通,咱走另外的路,总有解决方法的。我们可以把传入的字符串截取成多个字符然后传入到临时表中,然后使用游标或者直接关联表过滤数据。这样就可以达到后面预期的效果了。
下面我们以一个例子来具体实践一下:
1、创建数据库,用于实例:
create database huafeng_db;use huafeng_db;drop table if exists `huafeng_db`.`t_scores`;drop table if exists `huafeng_db`.`t_students`;drop table if exists `huafeng_db`.`t_class`;create table `huafeng_db`.`t_class` (  `class_id` int(11) not null,  `class_name` varchar(32) character set utf8 default null,  primary key (`class_id`)) engine=innodb default charset=utf8;insert into `huafeng_db`.`t_class` (`class_id`, `class_name`) values ('1', '一年级');insert into `huafeng_db`.`t_class` (`class_id`, `class_name`) values ('2', '二年级');insert into `huafeng_db`.`t_class` (`class_id`, `class_name`) values ('3', '三年级');insert into `huafeng_db`.`t_class` (`class_id`, `class_name`) values ('4', '四年级');insert into `huafeng_db`.`t_class` (`class_id`, `class_name`) values ('5', '五年级');insert into `huafeng_db`.`t_class` (`class_id`, `class_name`) values ('6', '六年级');create table `t_students` (  `student_id` int(11) not null auto_increment,  `student_name` varchar(32) not null,  `sex` int(1) default null,  `seq_no` int(11) default null,  `class_id` int(11) not null,  primary key (`student_id`),  key `class_id` (`class_id`),  constraint `t_students_ibfk_1` foreign key (`class_id`) references `t_class` (`class_id`)) engine=innodb default charset=utf8;insert into `huafeng_db`.`t_students`(`student_name`,`sex`,`seq_no`,`class_id`) values('小红',0,1,'1');insert into `huafeng_db`.`t_students`(`student_name`,`sex`,`seq_no`,`class_id`) values('小青',0,2,'2');insert into `huafeng_db`.`t_students`(`student_name`,`sex`,`seq_no`,`class_id`) values('小明',1,3,'3');insert into `huafeng_db`.`t_students`(`student_name`,`sex`,`seq_no`,`class_id`) values('小兰',0,4,'4');insert into `huafeng_db`.`t_students`(`student_name`,`sex`,`seq_no`,`class_id`) values('小米',1,5,'5');insert into `huafeng_db`.`t_students`(`student_name`,`sex`,`seq_no`,`class_id`) values('小白',1,6,'6');create table `huafeng_db`.`t_scores` (  `score_id` int(11) not null auto_increment,  `course_name` varchar(64) default null,  `score` double(3,2) default null,  `student_id` int(11) default null,  primary key (`score_id`),  key `student_id` (`student_id`),  constraint `t_scores_ibfk_1` foreign key (`student_id`) references `t_students` (`student_id`)) engine=innodb auto_increment=3 default charset=utf8;insert into `t_scores` (`score_id`, `course_name`, `score`, `student_id`) values ('1', '语文', '90', '1');insert into `t_scores` (`score_id`, `course_name`, `score`, `student_id`) values ('2', '数学', '97', '1');insert into `t_scores` (`score_id`, `course_name`, `score`, `student_id`) values ('3', '英语', '95', '1');insert into `t_scores` (`score_id`, `course_name`, `score`, `student_id`) values ('4', '语文', '92', '2');insert into `t_scores` (`score_id`, `course_name`, `score`, `student_id`) values ('5', '数学', '100', '2');insert into `t_scores` (`score_id`, `course_name`, `score`, `student_id`) values ('6', '英语', '98', '2');
2、需求: 根据学生编号批量删除学生信息
drop procedure if exists `p_del_studentinfo_byseqno`;delimiter $$create procedure p_del_studentinfo_byseqno(in arraystr varchar(1000),in ssplit varchar(10))sql security invoker  #允许其他用户运行begin    declare e_code int default 0;#初始化报错码为0    declare result varchar(256) character set utf8;#初始化返回结果,解决中文乱码问题    declare arrlength int default 0;/*定义数组长度*/    declare arrstring varchar(1000);/*定义初始数组字符*/    declare sstr varchar(1000);/*定义初始字符*/    declare continue handler for sqlexception set e_code=1;#遇到错误后继续执行;(需要返回执行结果时用这个)    start transaction;#启动事务    set arrlength = length(arraystr) - length(replace(arraystr,ssplit,''));/*获得数组长度*/    set arrstring = arraystr;    drop temporary table if exists list_tmp;    create temporary table list_tmp(id varchar(32));/*定义临时表*/    while arrlength > 0 do      set sstr = substr(arrstring,1,instr(arrstring,ssplit)-1);            -- 得到分隔符前面的字符串        set arrstring = substr(arrstring,length(sstr)+length(ssplit)+1);     -- 得到分隔符后面的字符串        set arrlength = arrlength -1;      set @str = trim(sstr);      insert into list_tmp(id) values(@str);     end while;     if row_count()=0 then          set e_code = 1;          set result = '请输入正确的参数';        end if;    set @count = (select count(1) from t_students s,list_tmp t where s.seq_no = t.id);    if @count >0 then        delete from t_scores where student_id in (select s.student_id from t_students s,list_tmp t where s.seq_no = t.id);        delete from t_students where student_id in (select t.id from list_tmp t);    else         set e_code = 1;         set result = '该学生不存在!';    end if;    if e_code=1 then        rollback;  #回滚    else        commit;        set result = '该学生已被删除成功';    end if;    select result;    drop temporary table if exists list_tmp;end $$delimiter ;
说明:在创建存储过程的时候,传入了两个参数,第一个代表要传入的数组字符串形式,第二个参数为以什么分割字符串。
声明初始化变量
declare arrlength int default 0;/*定义数组长度*/declare arrstring varchar(1000);/*定义初始数组字符*/declare sstr varchar(1000);/*定义初始字符*/
获取传入参数数组长度
set arrlength = length(arraystr) - length(replace(arraystr,ssplit,''));/*获得数组长度*/set arrstring = arraystr;/*赋值*/
创建临时表
drop temporary table if exists list_tmp;create temporary table list_tmp(id varchar(32));/*定义临时表*/
截取数组字符串并依次存入到临时表中供后面业务使用
while arrlength > 0 do  set sstr = substr(arrstring,1,instr(arrstring,ssplit)-1);            -- 得到分隔符前面的字符串    set arrstring = substr(arrstring,length(sstr)+length(ssplit)+1);     -- 得到分隔符后面的字符串    set arrlength = arrlength -1;  set @str = trim(sstr);  insert into list_tmp(id) values(@str);end while;
注: 存储过程结束时一定要记得删除临时表
不是非常复杂的业务没有必要用到存储过程的,本文不是引导大家一定要使用存储过程,只是让大家知道有这么一回事!
以上就是mysql怎么存数组的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product