May 23, 2008
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?
Adding Settings To Admin Pages
3 comments
page 793
Wp Polls Reviewed
one comment
page 58
3 Ways To Speed Up Your Blog Without A Cache Plugin
one comment
page 1321
Updating Code Snippets Here
one comment
page 1338
Wordpress Chat
3 comments
page 1308
Post Image The Easy Peasy Way
26 comments
page 1065
Post Image The Easy Peasy Way
26 comments
page 1065
Wpunlimited The Ultimate Wordpress Theme
3 comments
page 1141
Quick N Dirty Admin Login Screen
no comment
page 128
Wpunlimited The Ultimate Wordpress Theme
3 comments
page 1141
Post Image The Easy Peasy Way
26 comments
page 1065
Using Your Own Url Shortener
4 comments
page 1190
Quick N Dirty Category Redirection
no comment
page 133
Theming Habari Vs Wordpress
13 comments
page 440
Html 5 Gallery
6 comments
page 1305
Premium Ithemes Review Photo Gallery
4 comments
page 226
Updating Code Snippets Here
one comment
page 1338
Updating Code Snippets Here
one comment
page 1338
Html 5 Gallery
6 comments
page 1305
Post Image The Easy Peasy Way
26 comments
page 1065
Quick N Dirty Admin Login Screen
no comment
page 128
Post Image The Easy Peasy Way
26 comments
page 1065
Post Image The Easy Peasy Way
26 comments
page 1065
Html 5 Gallery
6 comments
page 1305
Post Image The Easy Peasy Way
26 comments
page 1065
Dont Mess With My Toot Toot
16 comments
page 599
Using Your Own Url Shortener
4 comments
page 1190
Quick N Dirty Admin Login Screen
no comment
page 128
Using Your Own Url Shortener
4 comments
page 1190
1 query every 899 seconds, updated 1 seconds ago.
(__)
`
Andrew
Thanks Jazz. I have resolved it now.
(__)
`
Jazz
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.
(__)
`
Andrew Rickmann
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.
(__)
`
Eric
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)
(__)
`
Eric
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.