Home > Database > Mysql Tutorial > body text

How to Pass a String of Values to an IN Clause in a Stored Procedure?

Susan Sarandon
Release: 2024-10-26 04:28:30
Original
915 people have browsed it

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');
Copy after login

If you wish to populate the IN clause values from a single variable containing a string of values, for instance:

'AA,BB,CC'
Copy after login
Copy after login

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'
Copy after login
Copy after login

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);
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!