MySQL stored procedure pass parameters where id in of 1 2 3... The sample

  • 2020-06-03 08:35:12
  • OfStack

Normal writing:
 
select * from table_name t where t.field1 in (1,2,3,4,...); 

When the list in write stored procedure in is substituted with an incoming parameter, the following method is used:

The find_in_set function is mainly used
 
select * from table_name t where find_in_set(t.field1,'1,2,3,4'); 

Of course, the more prosaic approach is to assemble the string and execute:
 
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); --  The connection string is generated to be executed SQL statements  
prepare stmt from @sentence; --  The projected release 1 Under.   " stmt The name of the predigested variable,  
execute stmt; --  perform SQL statements  
deallocate prepare stmt; --  Release resources  
END; 

Related articles: