I have a mysql table that looks something like this:
id | col_1 | col_2 | col_3
---|-------|-------|------
1 | 2 | 34 | 64
2 | 6 | 53 | 23
I would like to be able to query on the id and get multiple rows, one for each column. E.g:
SELECT column_name as column, column_value as value FROM my_table WHERE id=1;
Which would give me:
column | value
-------|-------
col_1 | 2
col_2 | 34
col_3 | 64
What would I need to use to formulate a query like this?
Many thanks
You could do it like this, this will return to you 2 comma separated first one of columns second of values, which you can explode and merge into KEY/VALUE arrays in PHP.
This is called a pivot. Actually it's a reverse pivot. See here for some background. http://www.artfulsoftware.com/infotree/queries.php#78
MySQL does it the hard way. It's a pain in the neck. Many people who do lots of this kind of work in MySQL use programs to generate these queries.