How to reuse variable from SQL SELECT statement in WHERE clause - mysql

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

Related

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

Group by and Having clause in the same query

Schema:
place(pid, name, type, lat, lng, deleted)
I want to select count of places, grouping them by their type and
having a distance of < 10 KM from a particular lat, lng
Query:
SELECT count(p.type) as count
FROM (place as p)
where p.deleted != 1
and p.pid in
(
select p2.pid,
IFNULL(acos(sin((18.5236 *pi()/180)) * sin((p2.lat*pi()/180))+cos((18.5236 *pi()/180)) * cos((p2.lat *pi()/180)) * cos(((73.8478 - p2.lng)*pi()/180))) * 6371.009, 0) AS distance
from place p2
having `distance` < 10
)
group by p.type;
Error:
Operand should contain 1 column(s)
That is because I am selecting 2 columns i.e pid and distance in the sub select query. But without using a 2nd select column how can I calculate the distance.
Rewrite your script like this
SELECT count(p.type) AS count,
-- remove this if not necessary
SUM(IFNULL(acos(sin((18.5236 *pi()/180)) * sin((p.lat*pi()/180))+cos((18.5236 *pi()/180)) * cos((p.lat *pi()/180)) * cos(((73.8478 - p.lng)*pi()/180))) * 6371.009, 0)) AS distance
FROM place AS p
WHERE p.deleted != 1
GROUP BY p.type
HAVING SUM(IFNULL(acos(sin((18.5236 *pi()/180)) * sin((p.lat*pi()/180))+cos((18.5236 *pi()/180)) * cos((p.lat *pi()/180)) * cos(((73.8478 - p.lng)*pi()/180))) * 6371.009, 0)) < 10

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.

MySQL calculating distance (simple solution)

I have the next query for getting addresses within a given distance and given postal code. Distance is calculated, based upon longitude and latitude data.
In this example i have replaced the user-input for just values (lat=52.64, long=6.88 en desired distance=10km)
the query:
SELECT *,
ROUND( SQRT( POW( ( (69.1/1.61) * ('52.64' - latitude)), 2) + POW(( (53/1.61) * ('6.88' - longitude)), 2)), 1) AS distance
FROM lp_relations_addresses distance
WHERE distance < 10
ORDER BY `distance` DESC
gives unknown column distance as error message.
when leaving out the where clausule i get every record of the table including their calculated distance. In this case i have to fetch the whole table.
How do i get only the desired records to fetch??
Thanks in advance for any comment!
You can't reference an alias in the select clause from another part of the sql statement. You need to put the whole expression in your where clause:
WHERE
ROUND( SQRT( POW( ( (69.1/1.61) * ('52.64' - latitude)), 2)
+ POW(( (53/1.61) * ('6.88' - longitude)), 2)), 1) < 10
A cleaner solution would be to use a sub-query to generate the calculated data:
SELECT *, distance
FROM (
SELECT *,
ROUND( SQRT( POW( ( (69.1/1.61) * ('52.64' - latitude)), 2)
+ POW(( (53/1.61) * ('6.88' - longitude)), 2)), 1) AS distance
FROM lp_relations_addresses
) d
WHERE d.distance < 10
ORDER BY d.distance DESC
Demo: http://www.sqlize.com/q96p2mCwnJ
As mellamokb notes, you can't reference column aliases in the WHERE clause. You can, however, do it in a HAVING clause:
SELECT *,
ROUND( SQRT( POW( ( (69.1/1.61) * ('52.64' - latitude)), 2) +
POW(( (53/1.61) * ('6.88' - longitude)), 2)), 1) AS distance
FROM lp_relations_addresses
HAVING distance < 10
ORDER BY distance DESC
Ps. If you have lots of addresses, you might want to consider optimizing the query by ruling out some of them early. For example, with suitable indexes, the following version might be considerably faster:
SELECT *,
ROUND( SQRT( POW( ( (69.1/1.61) * ('52.64' - latitude)), 2) +
POW(( (53/1.61) * ('6.88' - longitude)), 2)), 1) AS distance
FROM lp_relations_addresses
WHERE latitude > '52.64' - 10 / (69.1/1.61)
AND latitude < '52.64' + 10 / (69.1/1.61)
AND longitude > '6.88' - 10 / (53/1.61)
AND longitude < '6.88' + 10 / (53/1.61)
HAVING distance < 10
ORDER BY distance DESC
You are aliasing the calculation as 'distance', but you are also aliasing table 'lp_relations_addresses' as 'distance'. Try giving them a different name like this:
SELECT *,
ROUND( SQRT( POW( ( (69.1/1.61) * ('52.64' - latitude)), 2) + POW(( (53/1.61) * ('6.88' - longitude)), 2)), 1) AS distance
FROM lp_relations_addresses addr
WHERE distance < 10
ORDER BY `distance` DESC