0

I have the following SQL statement which selects companies from the database (with a stored lat/lng) and displays the nearest 5 locations to the customer's location. This is working perfectly:

$query = sprintf("SELECT company_name, address, telephone, fax, contact_email, website, url, latitude, longitude, (1.609344 * 3959 * acos( cos( radians('".$center_lat."') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('".$center_lng."') ) + sin( radians('".$center_lat."') ) * sin( radians( latitude ) ) ) ) AS distance FROM cmsms_module_compdir_companies WHERE status='published' AND latitude!='' AND longitude!='' ORDER BY distance limit 5 ",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat));

However, I'd like to limit the results to only those stores within X distance from the customer's location - say 50 kilometers. I thought I could add the bit in bold below:

$query = sprintf("SELECT company_name, address, telephone, fax, contact_email, website, url, latitude, longitude, (1.609344 * 3959 * acos( cos( radians('".$center_lat."') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('".$center_lng."') ) + sin( radians('".$center_lat."') ) * sin( radians( latitude ) ) ) ) AS distance FROM cmsms_module_compdir_companies WHERE status='published' AND latitude!='' AND longitude!='' AND distance<'50' ORDER BY distance limit 5 ", mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat));

...but this returns no results at all.

Any ideas?

1
  • @alditis - I tried this, made no difference. Commented Dec 2, 2012 at 16:11

3 Answers 3

4

You cannot use calculated value distance in where condition.

plz use having

where ....
having distance < 50

Btw : the unit of distance is kilometer?

Sign up to request clarification or add additional context in comments.

5 Comments

Please reference for use HAVING without GROUP BY?
Thanks, this is perfect and @chumkiu it seems to work without GROUP BY.
If someone give me a doc for this I could upvote this answer :-) Because does not works for me (maybe as different mysql version)
@chumkiu ^_^ try it yourself. HAVING without GROUP BY can run correctly. Mysql support this behavior. The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well.
@chumkiu doc link: dev.mysql.com/doc/refman/5.1/en/select.html search HAVING.
0

you can't use a column alias in the WHERE clause.

So you should use

 WHERE (1.609344 * 3959 * acos( cos( radians('".$center_lat."') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('".$center_lng."') ) + sin( radians('".$center_lat."') ) * sin( radians( latitude ) ) ) ) < 50

OR add a query select as table (not tested, please be patient)

SELECT tmpt.distance, company_name, address, telephone, fax, contact_email, website, url, latitude, longitude, 
   FROM (SELECT *your expression* FROM table_name) AS tmpt, 
   cmsms_module_compdir_companies
   WHERE  status='published' AND latitude!='' AND longitude!='' AND distance<'50' ORDER BY distance limit 5

1 Comment

@AndreasLinden mmh having works only for group by I think
0

You can only use aliases in ORDER BY, GROUP BY and in HAVING clauses. use HAVING in your query for distance Use your code like this :

$query = sprintf("SELECT company_name, address, telephone, fax, contact_email, website, url, latitude, longitude, (1.609344 * 3959 * acos( cos( radians('".$center_lat."') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('".$center_lng."') ) + sin( radians('".$center_lat."') ) * sin( radians( latitude ) ) ) ) AS distance FROM cmsms_module_compdir_companies WHERE status='published' AND latitude!='' AND longitude!='' HAVING  distance<50 ORDER BY distance limit 5 ", mysql_real_escape_string($center_lat),
    mysql_real_escape_string($center_lng),
    mysql_real_escape_string($center_lat));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.