Jump to content

[SOLVED] Using select ... as, and then using it in the WHERE


scotchegg78

Recommended Posts

HI Guys

 

I have hte following...


SELECT 60 * 1.1515 * DEGREES(ACOS(SIN(RADIANS(52.478)) * SIN(RADIANS(job_postcodes.Lat)) + COS(RADIANS(52.478))* COS(RADIANS(job_postcodes.Lat)) * COS(RADIANS((-1.741)-job_postcodes.Long)))) as distance, PostCode FROM job_postcodes WHERE distance < 30

 

however I get the error distance unknown column??

Order by distance works, but not the where??

 

thanks for any info or help

Link to comment
Share on other sites

Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined.

 

To do what you want, you need to use a HAVING clause -

 

...HAVING distance < 30

Link to comment
Share on other sites

thanks for the reply, i will try it, but i have it working now using this...

 

SELECT sub.distance,jp.PostCode FROM (SELECT 60 * 1.1515 * DEGREES(ACOS(SIN(RADIANS(52.478)) * SIN(RADIANS(jp.Lat)) + COS(RADIANS(52.478))* COS(RADIANS(jp.Lat)) * COS(RADIANS((-1.741)-jp.Long))))as distance FROM job_postcodes jp) sub, 
job_postcodes jp WHERE sub.distance < 20

 

however it takes 1.9 seconds , which i think is a bit long.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.