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.

Database Query

Does anyone have any ideas?



Wordpress title showing space?
no comment on page 1371

Wordpress fun?
one comment on page 1376

Live blogging plugin?
4 comments on page 1258

Wordpress 3 admin speed up?
4 comments on page 1321

Framework photoshop?
3 comments on page 296

Fun wp plugins?
one comment on page 1376

Habari vs wordpress?
12 comments on page 440

Wp tags vs categories?
12 comments on page 7

Wordpress rss seo?
one comment on page 1361

Photo albums html5?
6 comments on page 1305

Wordpress chat?
no comment on page 1308

Wordpress exif data?
12 comments on page 230

Css sidear tab?
2 comments on page 336

Wordpress theme html5 blueprint?
6 comments on page 1305

Wordpress shortcode in plugin?
no comment on page 236

Html 50 photo album?
6 comments on page 1305

Get the post attachement?
24 comments on page 1065

Wordpress plugin development 30?
one comment on page 1373

Wordpress plugin development 30?
one comment on page 1373

Disqus formatting?
7 comments on page 1175

Html5 photoalbum?
6 comments on page 1305

Html5 photoalbum?
6 comments on page 1305

Wordpress fun?
one comment on page 1376

Fun wordpress plugins?
one comment on page 1376

Url shortener ideas?
4 comments on page 1190

Url shortener ideas?
4 comments on page 1190

Html 5 photo gallery?
6 comments on page 1305

Multiple post navigation?
no comment on page 1147

Html5 photo galleries?
6 comments on page 1305

Adding images to a wordpress 3 post?
24 comments on page 1065

Html5 photo gallery code?
6 comments on page 1305

Wordpress multiple blog master?
one comment on page 1376

Wordpress 3 tableprefix?
one comment on page 1376

Wordpress 3 tableprefix?
2 comments on page 1374

Using wordpress as a framework?
2 comments on page 335

Single post image size?
24 comments on page 1065

Get featured image src wordpress?
24 comments on page 1065

Disqus wordpress mu?
7 comments on page 1175

Image gallery html 5?
6 comments on page 1305

Wordpress theimage?
24 comments on page 1065

Wpgetattachmentimagesrc size?
24 comments on page 1065
  every 1735s, 1s ago, in 0.02s.
 __
(__)
   `
 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.


0.01s