>데이터 베이스 >MySQL 튜토리얼 >MySQL 存储过程传参数实现where id in(1,2,3,...)示例_MySQL

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB원래의
2016-06-01 13:25:471094검색

bitsCN.com 正常写法:

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;
bitsCN.com

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.