mysql存储过程简单案例

管理员 发布于 6年前   351

1.多字段

CREATE DEFINER=`root`@`localhost` PROCEDURE `r404`(a int)

begin 

select * from cunzhu ;

SET @count = (select count(*) from cunzhu); 

select @count;

select count(*),name,age,home into @count,@name,@age,@home from cunzhu where id=a;

select @count,@name,@age,@home;

end;

call r404(2);

结果:

1.png


2.循环查询

CREATE DEFINER=`root`@`localhost` PROCEDURE `r405`()

begin 

declare i int default 1;

loop_label: loop

            select * from cunzhu where id=i;

            set i=i+1;

            if i>3 then 

               leave loop_label;

            end if;

            end loop;

end;

call r405();

结果:

2.png


3.动态拼接

CREATE DEFINER=`root`@`localhost` PROCEDURE `r408`()

begin 

set @aa = 'select name from cunzhu where id=2';

set @sentence = concat(@aa); 

prepare aa from @sentence;

execute aa;  

deallocate prepare aa;  

end;

call r408();

结果:

1.png

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成

是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客

      订阅博客周刊

文章标签

友情链接

HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群
侯体宗的博客