MySQL存储过程传参数实现whereidin(1,2,3,...)⽰例
⼀个MySQL 存储过程传参数的问题想实现例如筛选条件为:where id in(1,2,3,...),下⾯有个不错的⽰例,感兴趣的朋友可以参考下正常写法:
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))
BEGINmysql存储过程题目
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;