I'm getting the wrong kind of column names from this query ... for example, I'm getting the column name meta_value
for the "resource_email" and the "resource_phone" cells... they both have the column name of meta_value
SET @centerLat = '48.428289';
SET @centerLng = '-123.380585';
SELECT wp_posts.*, resource_email.meta_value, resource_phone.meta_value,
( 3959 * acos( cos( radians( @centerLat ) ) * cos( radians( lat.meta_value ) ) * cos( radians( lng.meta_value ) - radians(@centerLng) ) + sin( radians( @centerLat ) ) * sin( radians( lat.meta_value ) ) ) ) AS distance
FROM wp_posts
LEFT JOIN wp_postmeta AS lat
ON lat.post_id = wp_posts.id
AND lat.meta_key = 'bid_resource_lat'
LEFT JOIN wp_postmeta AS lng
ON lng.post_id = wp_posts.id
AND lng.meta_key = 'bid_resource_lng'
LEFT JOIN wp_postmeta AS resource_email
ON resource_email.post_id = wp_posts.id
AND resource_email.meta_key = 'bid_resource_primary_email'
LEFT JOIN wp_postmeta AS resource_phone
ON resource_phone.post_id = wp_posts.id
AND resource_phone.meta_key = 'bid_resource_primary_phone'
HAVING distance < 5
LIMIT 0 , 20
Not sure how I need to change this query in order to get the column names I really need.
Use AS
:
SELECT wp_posts.*,
resource_email.meta_value AS resource_email,
resource_phone.meta_value AS resource_phone,
...
FROM ...
LEFT JOIN wp_postmeta AS resource_email ...
LEFT JOIN wp_postmeta AS resource_phone ...
The "cells" you mention are actually tables (table aliases, to be exact)
If the database does not accept column aliases equal to table aliases, you need to use different table aliases (or different column names).