
I’m stuck on a SQL query for my next WordPress plugin. I have tried Google but I am coming up empty, perhaps becasue I don’t really know what to search for, so I am hoping that one you guys can help me out with some advice.
Here’s the problem:
I have a table and in it are two important columns (There are others but they don’t matter). The first is called ‘Names’ and the second is called ‘Values’
I want to run a query that will return a single row. Each column in that row will be have the name that is stored in the ‘Names’ column in the original table, and the value that is stored in the ‘Values’ column.
The diagram below should hopefully make it a little clearer.

Does anyone have any ideas?
(__)
`
Thanks Jazz. I have resolved it now.
(__)
`
Just browsing over and I saw this message..
Warning: Invalid argument supplied for foreach() in /home/fhlinux162/w/wp-fun.co.uk/user/htdocs/wp-content/plugins/fun_with_in_context_comments.php on line 340
Just to let you know.
(__)
`
Thanks Eric, I’ll take a look at that. The reason I want to do it in SQL instead of PHP is that I am doing a join with another query and I want to return a single result in a way that is consistent with the native WordPress query.
(__)
`
http://en.wikibooks.org/wiki/Programming:MySQL/Pivot_table
A pivot is what you were looking for, but my first way would work just as well and is a little more understandable. (IMO)
(__)
`
You are fundamentally changing your data model there. You could have 0..n columns returned, and i dont know how you would handle that in a static sql query without knowing the result first.
why not:
select names, values from table where id = 1
then
foreach(result) {
add it into an array
}
Your data model “Query Results” is really an array.
Hope that helps.