Home > Database > Mysql Tutorial > body text

How to pass a variable to an IN clause in SQL?

Linda Hamilton
Release: 2024-11-01 12:45:02
Original
463 people have browsed it

How to pass a variable to an IN clause in SQL?

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

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

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

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!

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!