MySQL IN () Operator
When querying a MySQL database, the WHERE IN () operator is often used to retrieve rows based on specific values in a column. For instance, the following query retrieves all rows from the "table" table where the "id" column matches any of the values (1, 2, 3, 4):
SELECT * FROM table WHERE id IN (1,2,3,4);
However, an issue arises when a record has multiple "id" values, such as 1 and 3. In such cases, the query may not return that row.
Solution
The query translates to the following OR conditions:
SELECT * FROM table WHERE id='1' or id='2' or id='3' or id='4';
Therefore, it will only return rows that match any of these conditions.
To resolve this, one option is to use the SET datatype for the "id" column. This allows you to store multiple values using comma-separated strings. You can then use the FIND_IN_SET() function to search for specific values:
SELECT * FROM table WHERE FIND_IN_SET('1', id);
This query will return all rows that have '1' as one of the values in the "id" column, regardless of any other values that may be present.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3