I’ve a WordPress database with following tables
posts
+--------------+
| Field |
+--------------+
| ID |
| post_author |
| post_title |
| post_type |
+--------------+
postmeta
+--------------+
| Field |
+--------------+
| meta_id |
| post_id |
| meta_key |
| meta_value |
+--------------+
and have records with meta_key
values latitude
and longitude
How can I write SQL to show latitude
and longitude
as result column names?
+--------------+
| Field |
+--------------+
| ID |
| post_author |
| post_title |
| latitude |
| longitude |
+--------------+
Below query is my starting point
SELECT ID, post_title, meta_key, meta_value FROM `wp_posts`
LEFT JOIN `wp_postmeta` on ID=post_id
WHERE post_type='place' AND (meta_key='latitude' OR meta_key='longitude')
ORDER BY ID ASC
It sounds like you want to
PIVOT
thelatitude
andlongitude
values. Unfortunately, MySQL does not have aPIVOT
function but you can replicate it using an aggregate function with aCASE
statement:See SQL Fiddle with Demo
You could use something like this: