0

I have some problems with transpose row to columns.

Site based on Wordpress
DataBase: MariaDB
Pivot functionality: off (i guess)

I have a sql table in this format:

FIELD_NAME  FIELD_VALUE
your-name   Nazar
location    Ukraine
your-email  uubukraine@gmail.com
telephone   0992312167
your-browser    OPERA

Can you tell me how to turn her into this?

Your-Name  Location Your-Email    telephone   your-browser
Nazar      Ukraine  u@gmail.com   0992312167     opera

I tried this code but it doesn't work correctly.

SELECT
    submit_time,
    MAX(IF(field_name = 'your-name',     field_value, NULL)) AS Requestor,
    MAX(IF(field_name = 'your-email',    field_value, NULL)) AS Email,
    MAX(IF(field_name = 'location',      field_value, NULL)) AS Company_Name,
    MAX(IF(field_name = 'telephone',      field_value, NULL)) AS Telephone,
FROM
    wpv4_contactic_submits
GROUP BY
    submit_time
Rick James
  • 135,179
  • 13
  • 127
  • 222
  • 2
    Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product (and there is no `IF` in "SQL"). Please add a [tag](https://stackoverflow.com/help/tagging) for the database product you are using. [Why should I tag my DBMS](https://meta.stackoverflow.com/questions/388759/) –  May 11 '20 at 09:45
  • Your sample data doesn't have a `submit_time`, so the query doesn't match your sample data. Also, explain what you mean by "it doesn't work correctly". – Gordon Linoff May 11 '20 at 10:55
  • 1
    @openshac . . . `PIVOT` is NOT a SQL keyword. `PIVOT` is bespoke (i.e. not standard) functionality supported by a handful of databases, primarily SQL Server. – Gordon Linoff May 11 '20 at 10:56
  • @Gordon Linoff That's why there's a problem. I can't use PIVOT to sort out – Moris Nazar May 11 '20 at 11:08
  • @a_horse_with_no_name Site based on Wordpress DataBase: MariaDB Pivot functionality: off (i guess) – Moris Nazar May 11 '20 at 11:09
  • @openshac I can't use PIVOT to sort. – Moris Nazar May 11 '20 at 11:10
  • Follow the `[pivot-table]` tag for many suggestions on code. – Rick James May 25 '20 at 04:12
  • For Pivoting in MariaDB, see See https://stackoverflow.com/a/56670844/1766831 – Rick James Nov 01 '21 at 02:28

0 Answers0