I am trying to insert multiple rows into wp_postmeta. I can’t figure out the correct SQL query to attain it.
The problem I am facing is that my current data looks like this:
Col 1 | Col 2 | Col 3 | Col 4 | Col5
----------------------------------------
A | B | C | D | E
F | G | H | I | J
And I need to insert it into wp_postmeta like this:
Col 1 | Col 2 | Col 3
-----------------------
A | txt | B
A | txt | C
A | txt | D
A | txt | E
F | txt | G
F | txt | H
F | txt | I
F | txt | J
The “txt” data is not a problem, I am just trying to figure out a way to “denormalize” each row in my source data and store it in four different rows in the other table.
You can unpivot the data using a
UNION ALL
query which takes the data from the columns and converts it into rows:See SQL Fiddle with Demo.
Once you have the data into this format, then you can
INSERT
it into another table.