How to use $query->add_where_expression in hook_views_query_alter I'm trying to use hook_views_query_alter() to select some nodes based on a calculation using two fields in that node.  I can't find much documentation or many examples that help me understand how this works.

I could use Views PHP, but this module is very buggy in Drupal 7.

I think I need to use $query->add_where_expression() but I can't work out how to use it.

My view is called "latlong", and as a test to see if I have the right approach, I've tried to select nodes based on the value of field_latitude. Copying other functions i've seen through googling, this is my best guess, but it's not working. I know one of my nodes has the value 53.9519 so if my syntax is right I should get a row back, but I don't:

function my_module_views_query_alter(&amp;$view, &amp;$query) {
  switch($view-&gt;name) {
    case 'latlong':  
      $query-&gt;add_where_expression(0,'field_data_field_latitude.field_latitude_value = 53.9519');      
      break;
  }
}


If I print_r($query-&gt;where[0]) I get: 

Array ( 
  [conditions] =&gt; 
    Array ( 
      [0] =&gt; 
        Array ( 
         [field] =&gt; field_data_field_latitude.field_latitude_value = 53.9519
         [value] =&gt; Array ( ) 
         [operator] =&gt; formula ) ) 
         [args] =&gt; Array ( ) 
         [type] =&gt; AND )


What am I doing wrong?
If you look at the documentation for views_plugin_query_default::add_where_expression(), you will notice the following note. (The emphasys is mine.)


  The caller is reponsible for ensuring that all fields are fully qualified (TABLE.FIELD) and that the table already exists in the query. Internally the dbtng method "where" is used.


Looking at this Stack Overflow question, I would get your code should be the following one.

function mymodule_views_query_alter(&amp;$view, &amp;$query) {
  switch($view-&gt;name) {
    case 'latlong':
      $alias = $query-&gt;add_table('field_data_field_latitude');  
      $query-&gt;add_where_expression(0, "$alias.field_latitude_value = :latitude", array(':latitude' =&gt; 53.9519);      
      break;
  }
}


Since the latitude is a floating point, I would use a comparison that is different from $alias.field_latitude_value = :latitude, for example checking the latitude is inside the range [$latitude - $epsilon, $latitude + $epsilon].