MySQL游标的使用

MySQL游标的使用

游标可以理解为指向数据表中某一行记录的指针,用来指向一个查询结果的某一行,然后通过程序对该行数据进行特定操作。游标每次只能读取一行数据,对于多条记录,需要反复读取,直到游标读取不到数据为止。

游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行相应的操作。

一、数据准备

创建以下三张表:stu(学生)、course(课程)、score(成绩),并输入数据:

create table stu(
    s_id int auto_increment,
    s_name char(20),
    sex char(2),
    age int,
    phone char(20),
    addr varchar(100),
    constraint pk_stu_sid primary key(s_id)
);

create table course(
    c_id int auto_increment,
    c_name char(20),
    credits int,
    constraint pk_course_cid primary key(c_id)
);

create table score(
    s_id int,
    c_id int,
    score int,
    constraint pk_score_sid_cid primary key(s_id,c_id)
);

为以上三张表插入必要的数据,命令如下:

insert into stu(s_id,s_name,sex,age,phone,addr)
values(1001,'张平','女',20,'13703735566','河南省新乡市'),(1002,'王刚','男',21,'13703714422','河南省郑州市'),
(1003,'张静静','女',19,'13803716666','河南省郑州市'),(1004,'王涛','男',20,'13703732211','河南省新乡市'),
(1005,'王鹏飞','男',19,'13723750122','河南省安阳市'),(1006,'王强军','男',21,'13736890126','河南省安阳市'),
(1007,'刘晶晶','女',21,'13782576666','河南省安阳市'),(1008,'张梅梅','女',18,'13688884412','河南省开封市');

insert into course
values(1,'数据库',4),(2,'数据结构',4),(3,'管理学',3),(4,'英语',4),(5,'电子商务',3);

insert into score 
values(1001,1,80),(1001,2,90),(1001,3,77),(1001,4,87),(1001,5,69),
(1002,1,87),(1002,2,67),(1002,3,78),(1002,4,98),(1002,5,78),
(1003,1,66),(1003,2,77),(1003,3,88),(1003,4,99),(1003,5,66),
(1005,1,81),(1005,2,83),(1005,3,62),(1005,4,68),(1005,5,72),
(1004,1,72),(1004,2,60),(1004,3,84),(1004,4,88),(1004,5,74);

二、游标的使用

游标的使用分四个步骤:(1)定义游标;(2)打开游标;(3)取出游标中的数据;(4)关闭游标。

1、定义游标

定义游标的语法如下:

declare 游标名称 cursor for select_statement;

2、打开游标

打开游标的语法如下:

open 游标名称;

3、遍历游标中的数据

遍历游标的语法如下:

fetch 游标名称 into 变量名称1,变量名称2,...;

4、关闭游标

关闭游标的语法如下:

close 游标名称;

5、指定当游标指向最后一条记录时要进行的操作

declare continue handler for NOT FOUND 语句;
或者:
declare exit handler for NOT FOUND 语句;

三、举例

统计女生的人数和平均年龄:在存储过程sp_stu中定义游标cur_stu,然后通过while循环取出每一个学生。然后进行判断:如果该学生的性别是女生,则进行计数,并把该学生的年龄累加到变量student_age_sum中。

delimiter //

create procedure sp_stu()
begin
   declare student_cnt int default 0;
   declare student_age_sum int default 0;

   declare student_name char(20) default '';
   declare student_sex char(2) default '';
   declare student_age int default 0;

   declare flag int default 1;

   declare cur_stu cursor for 
   select s_name,sex,age from stu;

   declare continue handler for not found set flag:=0;

   open cur_stu;
   fetch cur_stu into student_name,student_sex,student_age;

   while flag=1 do
       if student_sex='女' then 
          set student_cnt:=student_cnt+1;
          set student_age_sum:=student_age_sum+student_age;
       end if;
       fetch cur_stu into student_name,student_sex,student_age;
   end while;

   if student_cnt>0 then
       select student_cnt,student_age_sum/student_cnt as student_age_avg;
   else
       select '查询结果为空';
   end if;

   close cur_stu;

end //
delimiter ;

调用以上存储过程,查询结果如下:

mysql> call sp_stu();
+-------------+-----------------+
| student_cnt | student_age_avg |
+-------------+-----------------+
|           4 |         19.5000 |
+-------------+-----------------+
1 row in set (0.04 sec)``

Query OK, 0 rows affected (0.04 sec)
发布了44 篇原创文章 · 获赞 48 · 访问量 5389

猜你喜欢

转载自blog.csdn.net/weixin_44377973/article/details/103767867