2

I am trying to run subqueries from another table in a query

My Query is as follows:

SELECT *, (6371000 * acos(cos(radians(select point_oi.lng
                                     from point_oi
                                     where point_oi.name like '%Main Square%')
                            ) 
* cos(radians(restaurants.lat)) * cos(radians(restaurants.lng) 
- radians(select point_oi.lng
         from point_oi
         where point_oi.name like '%Main Square%'
        )) 
+ sin(radians(select point_oi.lng
             from point_oi
             where point_oi.name like '%Main Square%'))
* sin(radians(restaurants.lat)))) AS distance
FROM restaurants
HAVING distance < 500;

When I run the Query I get an error saying that there is an error near select. I would like to use the nested select queries to get the lat and lng from another table rather than hardcoding the values. How can I fix this.

Thank you for your help

1
  • 2
    You should use Where instead of HAVING in your query. Commented Nov 27, 2019 at 16:34

2 Answers 2

3

You should not use subquery for retrieve point_poi lat, lnt if the suquery return more than a rows you have error ..

try use a proper join (in this case do the fatc you have not relation between point_poi and restaurants you could use cross join )

  SELECT restaurants.*, 
    (6371000 * acos(cos(radians(point_oi.lng )) 
  * cos(radians(restaurants.lat)) * cos(radians(restaurants.lng) 
  - radians(point_oi.lng )) 
  + sin(radians(point_oi.lng ))
  * sin(radians(restaurants.lat)))) AS distance
  FROM restaurants
  CROSS JOIN point_oi 
  WHERE   point_oi.name like '%Main Square%'
  AND (6371000 * acos(cos(radians(point_oi.lng )) 
  * cos(radians(restaurants.lat)) * cos(radians(restaurants.lng) 
  - radians(point_oi.lng )) 
  + sin(radians(point_oi.lng ))
  * sin(radians(restaurants.lat)))) < 500;
Sign up to request clarification or add additional context in comments.

Comments

0

Please try this query and let me know is your issue resolve.

SELECT t.*
        ,(6371000 * acos(cos(radians(SELECT point_oi.lng FROM point_oi WHERE point_oi.name LIKE '%Main Square%')) * cos(radians(restaurants.lat)) * cos(radians(restaurants.lng) - radians(SELECT point_oi.lng FROM point_oi WHERE point_oi.name LIKE '%Main Square%')) + sin(radians(SELECT point_oi.lng FROM point_oi WHERE point_oi.name LIKE '%Main Square%')) * sin(radians(restaurants.lat)))) AS distance
FROM restaurants As t
WHERE distance < 500;

2 Comments

thanks, but I still get the same error and it seems to be at the first nested select
Subqueries need to be enclosed in parenthesis, so if you use it as an argument to a function, it needs look something like this: f((SELECT ...)). Also, for a subquery to be a valid function argument, its result must be a single row.

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.