Home >Database >Mysql Tutorial >How Can I Efficiently Denormalize Comma-Separated Lists in MySQL for Search Engine Integration?
Problem:
In an unnormalized table, a column harbors a comma-separated list that acts as a foreign key to a separate table. This creates a challenge when integrating the data into a search engine lacking procedural language support. The goal is to split this list into multiple rows, ultimately yielding a denormalized table.
Solution:
Although MySQL does not provide functions that inherently return tables, a clever solution can be implemented:
SELECT part_id, REGEXP_SUBSTR(`material`, '[^,]+', 1) AS `material_id` FROM unnormalized_table
This query assigns the first value in the comma-separated list to the material_id column, creating a new row for each part_id.
Example:
Applying this solution to the example provided in the problem statement would yield the following results:
part_id | material_id |
---|---|
339 | 1 |
339 | 2 |
970 | 2 |
Conclusion:
By leveraging a combination of string manipulation and subqueries, it is possible to denormalize comma-separated lists in MySQL. This technique enables the seamless integration of such data into various applications that do not support advanced procedural language features.
The above is the detailed content of How Can I Efficiently Denormalize Comma-Separated Lists in MySQL for Search Engine Integration?. For more information, please follow other related articles on the PHP Chinese website!