1

I would like to understand how I can turn these rows into columns in MySql

This is how the DB is saved:

id form_id entry_id meta_value
1 7 18 Name
2 7 18 Date
3 7 18 Phone
4 7 18 Email
5 7 18 Status
6 8 19 DOB
7 8 19 Address
8 8 19 Post Code
9 8 19 Country
10 8 19 Notes
11 7 20 Name
12 7 20 Date
13 7 20 Phone
14 7 20 Email
15 7 20 Status
16 8 21 DOB
17 8 21 Address
18 8 21 Post Code
19 8 21 Country
20 8 21 Notes

That is how the table is currently, the idea is combining both form_id (7 & 8) and as a row:

column 1 column 2 column 3 column 4 column 5 column 6 column 7 column 8 column 9 column 10
Name Date Phone Email Status DOB Address Post Code Country Notes
Name Date Phone Email Status DOB Address Post Code Country Notes

I have tried with INNER JOIN and Pivot but I am stuck all day on that. If you guys could help would be amazing.

Thanks in advanced

Here is the fiddle: https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=943eb14a4fd39476033e18eb0aa625e8

New contributor
Tiago Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
7
  • This is PIVOT. Not implemented. Use conditional aggregation.
    – Akina
    Sep 14 at 17:54
  • Hi, and welcome to dba.se! Please put your table and data into a fiddle (dbfiddle.uk) so that anyone who wishes to help can try out their solutions... help us to help you! It means that there is a single source of truth and means that there's no duplication of effort!
    – Vérace
    Sep 14 at 19:08
  • Is this strictly MySQL, or would you consider MariaDB as well? (Because there is a solution for this in MariaDB.)
    – dbdemon
    Sep 14 at 21:09
  • Follow the [pivot] tag.
    – Rick James
    Sep 15 at 1:15
  • Hi, @Vérace many thanks for your help, here is the fiddle: dbfiddle.uk/… Many thanks for your help :) Sep 15 at 8:07

Your Answer

Tiago Silva is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.