Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
How to create new Records from Concatenated Field in MySQL Table

I have a table 'Cases' with a field string field called 'Tags', which contains tags seperated by | character:

Cases

+----+-------------------+   
| Id |       Tags        |  
+----+-------------------+  
| 12 | "car|plane|truck" |  
| 11 | "plane"           |  
+----+-------------------+  

and I would like to create a new Table to map these Tags as follow:

Tags

+----+---------+-------+  
| Id | case_id |  tag  |  
+----+---------+-------+  
|  1 |      12 | car   |  
|  2 |      12 | plane |  
|  3 |      12 | truck |  
|  4 |      11 | plane |  
+----+---------+-------+  

How do I write an sql statement to create each record on the table as described above?

Thanks!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The solution is bit complicate:

set @id = 0;
select 
    @id:=(@id + 1) as id,
    id as case_id,
    JSON_UNQUOTE(
      JSON_EXTRACT(
        CONCAT('["', REPLACE(tags, '|', '", "'), '"]'), -- format string as JSIN array
        CONCAT('$[',pos,']')
      )
    ) tag
from Tags
join JSON_TABLE( -- join series pseudo table
    "[0,1,2,3]", -- this array length must be equal max tag elements length 
    "$[*]" 
    COLUMNS(pos varchar(255) PATH '$')
) as seq
    on seq.pos<JSON_LENGTH(CONCAT('["', REPLACE(tags, '|', '", "'), '"]'))
;

DB Fiddle example

id  case_id     tag
1   11          plane
2   12          car
3   12          plane
4   12          truck

P.S. This solution only for MySQL 8

over 4 years ago · Santiago Trujillo Report

0

I think a recursive CTE is a simplish way to solve this:

with recursive cte as (
      select id, concat(tag, '|') as rest, cast(null as char(255)) as tag, 1 as lev
      from tags
      union all
      select id, substr(rest, instr(rest, '|') + 1),
             substring_index(rest, '|', 1), lev + 1
      from cte
      where rest <> ''
     )
select id, tag
from cte
where lev > 1;

Here is a db<>fiddle.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!