当前位置:Gxlcms > mysql > MySQL存储过程传参数实现whereidin(1,2,3,...)示例

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

时间:2021-07-01 10:21:17 帮助过:20人阅读

正常写法:
代码如下:

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,2,3,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;

您可能感兴趣的文章:

  • MySQL左联多表查询where条件写法示例
  • MySQL Where 条件语句介绍和运算符小结
  • 解析mysql left( right ) join使用on与where筛选的差异
  • UCenter info: MySQL Query Error SQL:SELECT value FROM [Table]vars WHERE noteexists
  • MYSQL where 1=1判定中的作用说明
  • MySQL中的max()函数使用教程
  • MySQL性能优化之max_connections配置参数浅析
  • mysql中max_allowed_packet参数的配置方法(避免大数据写入或者更新失败)
  • mysql max 与 where 间的执行问题小结

人气教程排行