加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

MySQL 存储过程传参数实现where id in(1,2,3,...)示例

发布时间:2020-12-12 08:15:07 所属栏目:MsSql教程 来源:网络整理
导读:正常写法: 复制代码 代码如下: select * from table_name t where t.field1 in (1,2,3,4,...); 当在写存储过程in里面的列表用个传入参数代入的时候,就需要用到如下方式: 主要用到find_in_set函数 复制代码 代码如下: select * from table_name t where fin
正常写法:
复制代码 代码如下:
select * from table_name t where t.field1 in (1,2,3,4,...);

当在写存储过程in里面的列表用个传入参数代入的时候,就需要用到如下方式:

主要用到find_in_set函数
复制代码 代码如下:
select * from table_name t where find_in_set(t.field1,'1,4');

当然还可以比较笨实的方法,就是组装字符串,然后执行:
复制代码 代码如下:
DROP PROCEDURE IF EXISTS photography.Proc_Test;
CREATE PROCEDURE photography.`Proc_Test`(param1 varchar(1000))
BEGIN
set @id = param1;
set @sel = 'select * from access_record t where t.ID in (';
set @sel_2 = ')';
set @sentence = concat(@sel,@id,@sel_2); -- 连接字符串生成要执行的SQL语句
prepare stmt from @sentence; -- 预编释一下。 “stmt”预编释变量的名称,
execute stmt; -- 执行SQL语句
deallocate prepare stmt; -- 释放资源
END;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读