Home >Backend Development >PHP Tutorial >How to Query a Table with Comma-Separated Values in a Column?
Query Table with Multiple Comma-Separated Values in a Column
In this scenario, you have a table with a column that contains comma-separated values, and you're facing the challenge of querying that column for specific values. The dilemma arises when multiple values are stored in the same cell, making it difficult to locate a particular value with precision using simple SQL filters.
To address this issue, a viable approach is to normalize your database schema by creating a separate table for child names. This would allow you to join tables based on child IDs or names, enabling precise filtering and retrieval of data.
Alternatively, if restructuring the schema is not feasible, you can utilize the FIND_IN_SET function in your SQL query. This function allows you to search for a specific value within a comma-separated list stored in a column. By implementing this function, you can refine your query to identify rows where the desired value exists within the children column.
For instance, in your example, you can execute the following query to find parents who have a child named "Alex":
SELECT * FROM table_name WHERE FIND_IN_SET('Alex', children);
Using this approach, you can efficiently locate and retrieve data based on specific values stored in comma-separated columns, even when multiple names reside in the same cell.
The above is the detailed content of How to Query a Table with Comma-Separated Values in a Column?. For more information, please follow other related articles on the PHP Chinese website!