How can I run subqueries in mysql - mysql

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

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;

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;

Related

How to reuse variable from SQL SELECT statement in WHERE clause

I need to take out values from database , based on distance from any given point and then order by distance ascending. Longitude and latitudes are saved in database as float field. Here is my query
SELECT
*,
#distance : = 6371 * 2 * asin(sqrt(POW(sin(({lat} - radians(address.latitude)) / 2), 2) + cos({lat}) * cos(radians(address.latitude)) * POW(sin(({lon} - radians(address.longitude)) / 2), 2)))
FROM
service,
provider,
address
WHERE
service.provider_id = provider.id
AND provider.address_id = address.id
AND provider.status = True
AND
(
6371 * 2 * asin(sqrt(POW(sin(({lat} - radians(address.latitude)) / 2), 2) + cos({lat}) * cos(radians(address.latitude)) * POW(sin(({lon} - radians(address.longitude)) / 2), 2)))
)
< 10
order by
#distance
I need to reuse the distance for condition like where #distance < 10
but I'm unable to reuse, it returns empty list. whereas #distance in ORDER BY is working fine. How can i reuse variable in where clause ?
Assuming you do not need #DISTANCE outside the select, here's an approach to try:
SELECT *,
6371 * 2 * asin(sqrt(POW(sin(({lat} - radians(address.latitude)) / 2), 2) + cos({lat}) * cos(radians(address.latitude)) * POW(sin(({lon} - radians(address.longitude)) / 2), 2))) AS DISTANCE
FROM service,
provider,
address
WHERE
service.provider_id = provider.id
AND provider.address_id = address.id
AND provider.status = True
HAVING
DISTANCE < 10
ORDER BY
DISTANCE
Note that a I have given the calculation an alias DISTANCE(see far right of select) and used HAVING rather than WHERE for the reference to the alias.
Avoid using MySQL variables; they are obsolete in newer versions of MySQL. Also, it's better to use modern syntax join (SQL-92) instead of old joins.
Anyway, I think the following query produces what you want:
select *
from (
SELECT
*,
6371 * 2 * asin(sqrt(POW(sin(({lat} - radians(a.latitude)) / 2), 2)
+ cos({lat}) * cos(radians(a.latitude)) * POW(sin(({lon}
- radians(a.longitude)) / 2), 2))) as distance
FROM service s
JOIN provider p ON s.provider_id = p.id
JOIN address a ON p.address_id = a.id
WHERE p.status = True
) x
WHERE distance < 10
ORDER BY distance

Combining SQL Google Search with own SQL query

I am pretty new to SQL queries.
I have a google SQL Search example
SELECT cID,
(6371 * acos
(
cos(radians(51.455643))
* cos(radians(latCord))
* cos(radians(longCord) - radians(7.011555))
+ sin(radians(51.455643))
* sin(radians(latCord))
)
) AS distance
FROM breitengrade
HAVING distance < 50
ORDER BY distance
LIMIT 0, 20
and a own SQL query
SELECT breitengrade.cID
,breitengrade.latCord
,breitengrade.longCord
,Pages.cIsActive
FROM breitengrade
INNER JOIN Pages ON breitengrade.cID = Pages.cID
WHERE cIsActive = '1'
How can I combine these 2 queries into one so that I can get one single result set?
SELECT breitengrade.cID,
breitengrade.latCord,
breitengrade.longCord,
Pages.cIsActive
(6371 * acos
(
cos(radians(51.455643))
* cos(radians(latCord))
* cos(radians(longCord) - radians(7.011555))
+ sin(radians(51.455643))
* sin(radians(latCord))
)
) AS distance
FROM breitengrade
INNER JOIN Pages ON breitengrade.cID = Pages.cID
WHERE cIsActive = '1'
HAVING distance < 50
ORDER BY distance
LIMIT 0, 20

SQL ORDER BY distance not working properly

For the result on my page I need to order the results by distance. The page gets the current position of the user and retrieves the closest 30 results of that position.
Next to the order of the distance I also want to order the results for every venue by price.
Problem is that the price order works, but not the distance order. I can't figure out how my function is ordering the results because there is no structure in it (sometimes the closest result is on the last placing, sometimes it is in the middle, sometimes in the beginning, it's really weird).
My SQL statement looks like that:
$lat = '48.213688';
$lng = '16.408229';
$sql = "SELECT *, (3959 * acos(cos(radians('".$lat."')) * cos(radians(venue_address_lat)) * cos(radians(venue_address_lng) - radians('".$lng."')) + sin(radians('".$lat."')) * sin(radians(venue_address_lat)))) AS distance
FROM car_offer JOIN venues_infos ON car_offer.venues_infos_id = venues_infos.venue_id WHERE (3959 * acos(cos(radians('".$lat."')) * cos(radians(venue_address_lat)) * cos(radians(venue_address_lng) - radians('".$lng."')) + sin(radians('".$lat."')) * sin(radians(venue_address_lat)))) < 1.5
AND car_offer.offer_date = CURDATE()
AND venue_address_postcode = 1020
OR car_offer.offer_date = '0000-00-00'
ORDER BY distance, offer_price_small LIMIT 0,30";
The lat, lng in the database is from Google Maps, so this should be accurate.
I think you are better of using a outer query in this case and then do the order by in outer query like below. (OR) you can use the same distance calculation expression in your order by. Notice I have moved the distance < 1.5 from inner to outer query. Moreover, I am in doubt whether you can use distance in your order by clause like the way you are using (mean in the same query level unless it's from a derived table expression) since distance is a column alias.
SELECT * FROM
(
SELECT *, (3959 * acos(cos(radians('".$lat."')) * cos(radians(venue_address_lat)) * cos(radians(venue_address_lng) - radians('".$lng."')) + sin(radians('".$lat."')) * sin(radians(venue_address_lat)))) AS distance
FROM car_offer
JOIN venues_infos
ON car_offer.venues_infos_id = venues_infos.venue_id
WHERE car_offer.offer_date = CURDATE()
AND venue_address_postcode = 1020
OR car_offer.offer_date = '0000-00-00'
) tab
WHERE distance < 1.5
ORDER BY distance, offer_price_small
LIMIT 0,30

Different calculation figure in arithmetic TSQL

I am getting diff. figures in my below equation written.
SELECT 9.36 + 9.36 / ( 284.36 ) * 15.64 = 9.8748065528 (CORRECT)
SELECT 18.72 / ( 284.36 ) * 15.64 = 1.0296131056 (INCORRECT)
I have total (9.36 * 2) Which I am putting in second select statement and gives incorrect amount.
What I am doing wrong?
The equations are not the same.
The second statement is the equivalent of:
SELECT (9.36 + 9.36) / ( 284.36 ) * 15.64
not
SELECT 9.36 + 9.36 / ( 284.36 ) * 15.64
Remember your Order of Operations.
THIS is what I want.
SELECT 9.36 + (9.36 / ( 284.36 ) * 15.64)

How to lookup nearest location (Latitude / Longitude) in a join query

I have a table with a list of users, which looks something like;
Users.. ( id, username, lat, long )
I have a location table which looks like this:
Locations.. ( id, name, lat, lng, bound_ne_lat, bound_ne_lng, bound_sw_lat, bound_sw_lng
What I'm trying to do, is something like;
SELECT Users.*, Locations.name as NearestTown
FROM Users
INNER JOIN Locations ON ( .. lookup nearest town from Locations based on Users lat lng coords, return only the nearest result .. )
I did think about doing some kind of subquery, like;
INNER JOIN Locatios ON Locations.id IN (SELECT id FROM Locations ORDER BY (..distance query) desc limit 1
But then I found out I couldn't pass the users lat / lng into to the sub query for each result.
The current formula I do use however to calculate the distance between the 2 points is;
(3956 * 2 * ASIN(SQRT( POWER(SIN((#sourceLat - table.lookupLat) * pi()/180 / 2), 2) +COS(#sourceLat * pi()/180) * COS(table.lookupLat * pi()/180) * POWER(SIN((#sourceLng - table.lookupLng) * pi()/180 / 2), 2) ))) as Distance,
However, how could I use this in a subquery [if the best option] when I cannot pass in the #sourceLat and #sourceLng for each result .. ?
Any help, most appreciated.