Mysql Get Column Name after Case Max i have this scenario with this query that trasform multiple rows key/values in a columns.

SELECT
  t1.*, t2.*, u.name, u.email, u.username, 
  MAX(CASE WHEN t2.datakey = 'latitude' THEN t2.data END) AS latitude,
  MAX(CASE WHEN t2.datakey = 'longitude' THEN t2.data END)AS longitude
FROM `#_social_fields_data` AS t1 
INNER JOIN `#_social_fields_data` AS t2 ON t1.uid  = t2.uid
LEFT JOIN `f9leh_users` AS u ON t1.uid = u.id
GROUP BY t1.uid


and i need to add a where clause to call the latitude value
but adding clause for example 

where latitude &gt; 0 


and after i need to call the colum to replace the new a.lat with latitude column name

 $distance_col_expression = "(((acos(sin((".$post['socialmembers']['location_lat']."*pi()/180)) *
                            sin((a.lat * pi()/180))+cos((".$post['socialmembers']['location_lat']." * pi()/180)) *
                            cos((a.lat * pi()/180)) * cos(((".$post['socialmembers']['location_lng']."- a.lng)
                            *pi()/180))))*180/pi())*60*1.1515) 
                            ";
                        $query .= ' AND '.$distance_col_expression .' &lt;= '.$post['socialmembers']['search_radius'];


it not work. Says invalid column name. I have tried with other names but seems that this name generated is temporary.

Please can suggest me, How can i do?
Thanks in advance
Thanks for the useful suggestion.

Now the query work as in this way:

select DISTINCT t3.uid,latitude,longitude
from (
  SELECT DISTINCT 
  t2.*, 
  MAX(CASE WHEN t2.datakey = 'latitude' THEN t2.data END) AS latitude,
  MAX(CASE WHEN t2.datakey = 'longitude' THEN t2.data END)AS longitude
FROM `#_social_fields_data` AS t1 
INNER JOIN `#_social_fields_data` AS t2 ON t1.uid  = t2.uid
GROUP BY t1.uid
     ) as t3
WHERE latitude &gt; 0 


Thanks, as suggested i have optimized the query removing self_join:

select DISTINCT t2.uid,latitude,longitude
from (
  SELECT DISTINCT 
  t1.*, 
  MAX(CASE WHEN t1.datakey = 'latitude' THEN t1.data END) AS latitude,
  MAX(CASE WHEN t1.datakey = 'longitude' THEN t1.data END)AS longitude
FROM `#_social_fields_data` AS t1 
GROUP BY t1.uid
     ) as t2
WHERE latitude &gt; 0