I’m using WordPress and trying to create a view to make reviewing and analyzing my resort data a little easier. Resort data is stored in the post_meta table in WordPress and referenced in a custom post type known as “resorts”. The following query gives me the result set I want to parse:
SELECT a.id, a.post_type, a.post_title, b.post_id, b.meta_key, b.meta_value
FROM alpinezone_postmeta b
INNER JOIN alpinezone_posts a ON a.id = b.post_id
WHERE a.post_type = "resorts"
What I want to do with this result set is have each unique meta_key of a set I define become a column and then each row should be a unique b.post_id (or a.id), which corresponds to an individual resort’s record.
So ultimately I end up with:
post_title | phone_num | state |
resort1 | 800-200-1111 | Vermont |
resort2 | 800-200-2222 | New Hampshire |
resort 3 | 800-200-2323 | Maine |
Basically …… I’m not that great at MySQL so trying to figure out the best way to handle this. I do have a list of all the meta_key I want to place into columns, there are 36 of them capturing a range of information.
EDIT: Some more detail.
Current Structure – shows what table it comes from as well
*alpinezone_posts alpinezone_postmeta alpinezone_postmeta*
post_title meta_key meta_value
----------------------------------------------------------------
sugarloaf snow_phone 888-234-2222
sugarloaf vertical_feet 2300
sugarloaf site_url sugarloaf.com
wachusett snow_phone 888-111-2222
wachusett vertical_feet 1000
wachusett site_url wachusett.com
These two tables are joined on post_id from table alpinezone_postmeta and id from table alpinezone_posts.
Only want results where the post_type in table alpinezone_posts is = “resorts”
How I want it to look in new view
post_title snow_phone vertical_feet site_url
-------------------------------------------------------
sugarloaf 888-234-2222 2300 sugarloaf.com
wachusett 888-111-2222 1000 wachusett.com
You want to use is a cross-tabulated table or pivot table with “GROUP BY” like this example here: http://en.wikibooks.org/wiki/MySQL/Pivot_table
You need to use something other then SUM() for strings though… like MAX():
http://forums.mysql.com/read.php?20,75357,75367#msg-75367