Searching for Multiple Values in a Single Field using SQL
In SQL, searching for multiple values in the same field can be achieved using either the IN or OR operators.
Using the IN Operator
The IN operator allows you to specify a set of distinct values to match against a field. For example:
<code class="sql">SELECT name FROM products WHERE name IN ('Value1', 'Value2', ...);</code>
This query will return all rows where the name field contains any of the specified values.
Using the OR Operator
The OR operator is used to combine multiple conditions, any of which can be true. To search for multiple values using OR, you would use the following syntax:
<code class="sql">SELECT name FROM products WHERE name LIKE '%Value1' OR name LIKE '%Value2';</code>
This query will return all rows where the name field contains either Value1 or Value2.
Comparison to Your Example
In your example, you are using the AND operator, which requires all conditions to be true. To achieve the desired result, you should use the OR operator instead:
<code class="sql">SELECT name FROM products WHERE name LIKE '%$search[1]%' OR name LIKE '%$search[2]%';</code>
This modification will cause the query to return results that match either of the specified values.
The above is the detailed content of How to Search for Multiple Values in a Single Field using SQL?. For more information, please follow other related articles on the PHP Chinese website!