I have a table that has 130+ columns and need to display the column names and the row field value for it side by side for record. Could someone help please me with the MySQL statement for it? The following will help clarify the output that I am looking for:
In Database:
A B C D E <- Column Name 1 2 3 4 5 <- Value
Output format needed in resultset:
Field - Value A 1 B 2 C 3 D 4 E 5
Using the following code, I was able to get the list of column names but don't know how to get the values associated with it:
SELECT c.column_name as qid FROM information_schema.columns c -- this has the column names WHERE c.table_name = 'TABLE NAME'
Thanks in advance.
Use a UNION
UNION
SELECT 'A' AS Field, A as Value FROM tablename UNION ALL SELECT 'B', B FROM tablename UNION ALL SELECT 'C', C FROM tablename UNION ALL SELECT 'D', D FROM tablename UNION ALL SELECT 'E', E FROM tablename
2.1m questions
2.1m answers
60 comments
57.0k users