Passing a Variable to an IN Clause
To pass a variable to an IN clause, you can use the FIND_IN_SET function. This function checks if a value is present in a comma-separated list.
For example, let's say you have the following stored procedure that selects product information based on product type:
SELECT product_id, product_price FROM product WHERE product_type IN ('AA','BB','CC');
You want to pass the values to the IN clause through a single variable, such as:
SELECT product_id, product_price FROM product WHERE product_type IN (input_variables);
To make this work, you can pass the parameter value as a comma-separated string, such as 'AA,BB,CC'. Then, use the FIND_IN_SET function to check if the product type is present in the input variable:
SELECT product_id, product_price FROM product WHERE FIND_IN_SET(product_type, param);
Where "param" is the name of the parameter that contains the comma-separated list of values. This will return the products whose type matches any of the values in the input variable.
The above is the detailed content of How to pass a variable to an IN clause in SQL?. For more information, please follow other related articles on the PHP Chinese website!