首页 技术 正文
技术 2022年11月7日
0 收藏 527 点赞 860 浏览 1179 个字

mysql不支持数组。但有时候需要组合几张表的数据,在存储过程中,经过比较复杂的运算获取结果直接输出给调用方,比如符合条件的几张表的某些字段的组合计算,mysql临时表可以解决这个问题.临时表:只有在当前连接情况下, TEMPORARY 表才是可见的。当连接关闭时, TEMPORARY 表被自动取消。必须拥有 create temporary table 权限,才能创建临时表。可以通过指定 engine = memory; 来指定创建内存临时表。

先建立要用的数据表及数据:

drop table if exists  person;create table `person` (  `id` )',  `age` ) DEFAULT NULL,  `name` ) not null) engine=innodb default charset=utf8;,,,,,,,,,,'test');

临时表支持主键、索引指定。在连接非临时表查询可以利用指定主键或索引来提升性能。存储过程语句及游标和临时表综合实例:

drop procedure if exists sp_test_tt; -- 判断存储过程函数是否存在如果是删除delimiter ;;create procedure  sp_test_tt()begin         create temporary table if not exists tmp   -- 如果表已存在,则使用关键词 if not exists 可以防止发生错误         (           id ) ,           name ),           age )         ) engine = memory;         begin        declare ids int; -- 接受查询变量        ); -- 接受查询变量        declare done int default false; -- 跳出标识        ); -- 接受查询变量        declare cur cursor for select id from person; -- 声明游标        declare continue handler for not FOUND set done = true; -- 循环结束设置跳出标识        open cur; -- 开始游标        LOOP_LABLE:loop -- 循环            FETCH cur INTO ids;            select name into names from person where id=ids;            select age into ages from person where id=ids;            insert into tmp(id,name,age) value(ids,names,ages);            if done THEN  -- 判断是否继续循环如果done等于true离开循环                LEAVE LOOP_LABLE; -- 离开循环            END IF;            end LOOP; -- 结束循环        CLOSE cur; -- 关闭游标     select * from tmp; -- 查询临时表         end;         truncate TABLE tmp;   -- 使用 truncate TABLE 的方式来提升性能end;;;delimiter ;;

执行存储过程:

call sp_test_tt();
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903