Inside phpMyAdmin, I am working with custom meta fields in WordPress.
Say I have custom meta for 3 fields- Address, Latitude, Longitude and wish to run an SQL query to display only these 3 columns with their values as rows below.
I can only figure out how to get one of the columns to show, and it’s data by running this:
SELECT DISTINCT wp_postmeta.meta_value AS address
FROM wp_postmeta, wp_posts
WHERE post_type = ‘dealers’
AND wp_postmeta.meta_key = ‘_dealer_address’
Could someone point me in a direction of how to run the query to include all 3 columns?
What I’m trying to do is mimic the setup of the table in this: http://code.google.com/apis/maps/articles/phpsqlsearch_v3.html#outputxml
Because I have a custom post type (dealers) using your custom metabox for latitude and longitude.
Your SQL query tested just fine in phpMyAdmin and looked identical to my other manually-created test dealer table, but when I tried to replace the google maps example query in the PHP- it was a no-go.
Is it too complicated of a query you think?
This is the test query that works without using the wordpress custom post:
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
Definitely stay away from distinct. 99% of the time, it’s a sign that something’s wrong (with the schema or your understanding of the schema).
I think that Cheluis’ suggestion might not associate the lat/long with the address.
I don’t have a wordpress installation to play with. I really hope that there’s a primary key (say, post_id). You don’t use it to join the two tables together in your original example, which is probably why you’re using the distinct.
If this id exists, you can join back to the wp_postmeta table several times, e.g.
This assumes that each dealer will have all three attributes. If not, you’ll need to switch to an outer join, which will make the query even more ugly.
Hope that helps.