MySQL - How to convert columns to rows?
P粉533898694
2023-07-24 23:17:01
<p><br /></p>
<pre class="brush:php;toolbar:false;">ID | a | b | c
1 | a1 | b1 | c1
2 | a2 | b2 | c2</pre>
<p>How to reorganize rows into IDs, column headers, values? </p>
<pre class="brush:php;toolbar:false;">1 | a1 | a
1 | b1 | b
1|c1|c
2 | a2 | a
2 | b2 | b
2 | c2 | c</pre>
<p><br /></p>
You are trying to reverse the data. MySQL does not have a reverse function, so you need to use a UNION ALL query to convert columns to rows:
See SQL Fiddle with Demo.
This can also be achieved using CROSS JOIN:
See SQL Fiddle with Demo