Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>SQL doesn't typically allow you to reference column aliases in WHERE, GROUP BY or HAVING clauses. MySQL does support referencing column aliases in the GROUP BY and HAVING, but I stress that it will cause problems when porting such queries to other databases.</p> <p>When in doubt, use the actual column name:</p> <pre><code>SELECT t.lat AS latitude FROM poi_table t WHERE t.lat &lt; 500 </code></pre> <p>I added a table alias to make it easier to see what is an actual column vs alias.</p> <h2>Update</h2> <hr> <p>A computed column, like the one you see here:</p> <pre><code>SELECT *, ( 6371*1000 * acos( cos( radians(42.3936868308) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(-72.5277256966) ) + sin( radians(42.3936868308) ) * sin( radians( lat ) ) ) ) AS distance FROM poi_table WHERE distance &lt; 500; </code></pre> <p>...doesn't change that you can not reference a column alias in the WHERE clause. For that query to work, you'd have to use:</p> <pre><code>SELECT *, ( 6371*1000 * acos( cos( radians(42.3936868308) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(-72.5277256966) ) + sin( radians(42.3936868308) ) * sin( radians( lat ) ) ) ) AS distance FROM poi_table WHERE ( 6371*1000 * acos( cos( radians(42.3936868308) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(-72.5277256966) ) + sin( radians(42.3936868308) ) * sin( radians( lat ) ) ) ) &lt; 500; </code></pre> <p>Be aware that using a function on a column (IE: <code>RADIANS(lat)</code>) will render an index useless, if one exists on the column.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload