Home >Database >Mysql Tutorial >How to Pass a String of Values to an IN Clause in a Stored Procedure?
Passing Variables to an IN Clause
Consider a stored procedure with a SELECT statement that includes an IN clause:
SELECT product_id, product_price FROM product WHERE product_type IN ('AA','BB','CC');
If you wish to populate the IN clause values from a single variable containing a string of values, for instance:
'AA,BB,CC'
You may encounter difficulties using this approach. Here's a solution:
Instead of attempting to pass the string directly to the IN clause, pass it as a parameter value in the following format:
'AA,BB,CC'
Utilize the FIND_IN_SET function to determine whether each product_type value is present in the input parameter:
SELECT product_id, product_price FROM product WHERE FIND_IN_SET(product_type, param);
In this modified statement, param represents the input parameter containing the string of values.
The above is the detailed content of How to Pass a String of Values to an IN Clause in a Stored Procedure?. For more information, please follow other related articles on the PHP Chinese website!