Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

MySQL: How to display column name and row field value side by side

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.

question from:https://stackoverflow.com/questions/65929056/mysql-how-to-display-column-name-and-row-field-value-side-by-side

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use a 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...