How to use hook_views_query_alter to add field with no table in Drupal 7 Views 3 I am new to Drupal 7/Views 3 hooks. I am trying to approximate this query:

SELECT *, 3956 * 2 * ASIN(SQRT(POWER(SIN(($postcode_lat - abs(lat))*pi()/180/2),2) + COS($postcode_lat * pi()/180) * COS(abs(lat) * pi()/180) * POWER(SIN(($postcode_lng - lng) * pi()/180/2),2))) AS distance 
WHERE distance &lt; 500
ORDER BY distance ASC


This gives a list of locations sorted by distance. However, in trying to implement with hooks, I am struggling. Here is the code that I tried:

$query-&gt;addField('', '3956 * 2 * ASIN(SQRT(POWER(SIN((32.7828 - abs(29.7631))*pi()/180/2),2) + COS(32.7828 * pi()/180) * COS(abs(29.7631) * pi()/180) * POWER(SIN((96.8039 - 95.2665) * pi()/180/2),2)))', 'distance');


Drupal doesn't like the empty parameter for table. But how can I add a field with no table?

I appreciate any suggestions offered.
I had the same problem. And I've solved it.

    function ModuleName_views_query_alter(&amp;$view, &amp;$query) {
  if ($view-&gt;name == 'my_view' &amp;&amp; 'my_block_name' == $view-&gt;current_display)) {


    $formula = "3956 * 2 * ASIN(SQRT(POWER(SIN(($postcode_lat - abs(lat))*pi()/180/2),2) + COS($postcode_lat * pi()/180) * COS(abs(lat) * pi()/180) * POWER(SIN(($postcode_lng - lng) * pi()/180/2),2)))";

    // Sets the formula's alias.
    $query-&gt;add_field(NULL, $formula, 'distance');
    $query-&gt;add_where(0, "$formula &lt; 500", array(), 'formula');

    // Sets the order by reference
    $query-&gt;orderby = array(
      array(
        'field' =&gt; 'distance',
        'direction' =&gt; 'ASC',
      )
    );
  }
}


See http://roldanarts.com/2013/11/22/diego-f-roldan/blog/using-formulas-views-hook-views-query-alter for more details.