I'm trying to do this query:
SELECT *, ( 6371 * acos( cos( radians(43.656906) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-79.434356) ) + sin( radians(43.656906) ) * sin( radians( latitude ) ) ) ) AS distance FROM Locations HAVING distance < 10 AND HAVING category='%Family%'
But I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'HAVING category='%Family%' LIMIT 0, 30' at line 1
Does anybody know what is the problem?
I hope category is column in your table
SELECT *, ( 6371 * acos( cos( radians(43.656906) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-79.434356) ) + sin( radians(43.656906) ) * sin( radians( latitude ) ) ) ) AS distance FROM Locations where category like '%Family%' HAVING distance < 10
You can not add having conditions twice .You can use "AND" inside the "Having" condition.So the code will be :
SELECT *, ( 6371 * acos( cos( radians(43.656906) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-79.434356) ) + sin( radians(43.656906) ) * sin( radians( latitude ) ) ) ) AS distance FROM Locations HAVING distance < 10 AND category='%Family%'
You need to remove the second HAVING and just use AND to tell MySQL that both conditions must hold.
SELECT *, ( 6371 * acos( cos( radians(43.656906) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-79.434356) ) + sin( radians(43.656906) ) * sin( radians( latitude ) ) ) ) AS distance FROM Locations HAVING distance < 10 AND category='%Family%'
Related
SELECT id,
( 6371 *
ACOS(
COS( RADIANS( db_latitude ) ) *
COS( RADIANS( $user_latitude ) ) *
COS( RADIANS( $user_longitude ) -
RADIANS( db_longitude ) ) +
SIN( RADIANS( db_latitude ) ) *
SIN( RADIANS( $user_latitude) )
)
)
AS distance FROM the_table HAVING distance <= the_table.SOME_COLUMN ORDER BY distance ASC"
I want to convert this query in sequelize-typescript.
I am trying to to build a basic sql query where I have a table of petrol stations and another table of POI in my sql database and I would like to get all petrol stations within a radius of a POI
I have the following query:
SELECT *, ( 6371000 * acos( cos( radians(15.4383252) )
* cos( radians( petrol_stations.lat ) ) * cos( radians( petrol_stations.lng ) - radians(47.0450591) )
+ sin( radians(15.4383252) ) * sin(radians(petrol_stations.lat)) ) )
AS distance FROM petrol_stations HAVING distance < 500
and it seems to work fine, however I have to hard-code the coordinates of the POI into the query. Is it possible to adapt the query such that the POI coordinates are pulled from the other table if provided with the name such as main square?
Thanks in advance.
assuming you have a georef poi table as
table_poi
id name lat lng
1 my_poi 47.0450591 15.4383252
you could try a cross join for related all your petrol_stations with all your POI
SELECT *
, ( 6371000 * acos( cos( radians(table_poi.lng) )
* cos( radians( petrol_stations.lat ) ) * cos( radians( petrol_stations.lng )
- radians(table_poi.lat) )
+ sin( radians(table_poi.lng) ) * sin(radians(petrol_stations.lat)) ) )
AS distance
FROM petrol_stations
CROSS JOIN table_poi
HAVING distance < 500
or using where (as requested by MarlinPierce)
SELECT *
, ( 6371000 * acos( cos( radians(table_poi.lng) )
* cos( radians( petrol_stations.lat ) ) * cos( radians( petrol_stations.lng )
- radians(table_poi.lat) )
+ sin( radians(table_poi.lng) ) * sin(radians(petrol_stations.lat)) ) )
AS distance
FROM petrol_stations
CROSS JOIN table_poi
WHERE ( 6371000 * acos( cos( radians(table_poi.lng) )
* cos( radians( petrol_stations.lat ) ) * cos( radians( petrol_stations.lng )
- radians(table_poi.lat) )
+ sin( radians(table_poi.lng) ) * sin(radians(petrol_stations.lat)) ) ) < 500
and if you want filter for a POI name
SELECT *
, ( 6371000 * acos( cos( radians(table_poi.lng) )
* cos( radians( petrol_stations.lat ) ) * cos( radians( petrol_stations.lng )
- radians(table_poi.lat) )
+ sin( radians(table_poi.lng) ) * sin(radians(petrol_stations.lat)) ) )
AS distance
FROM petrol_stations
WHERE table_poi.name like '%your_poi_name%'
CROSS JOIN table_poi
HAVING distance < 500
I am using the MySQL query to find distance between tow zip-codes when lat long is given.
SELECT postcode, ( 3959 * acos( cos( radians( 52.47592 ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( -1.90539 ) ) + sin( radians( 52.47592 ) ) * sin( radians( latitude ) ) ) ) AS distance FROM ukpostcodes HAVING distance <= 5 ORDER BY distance
it is working fine. But I want only postcode in result. I don't want distance column in result .
How can I achieve it.
Thanks
SELECT postcode FROM(
SELECT postcode, ( 3959 * acos( cos( radians( 52.47592 ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( -1.90539 ) ) + sin( radians( 52.47592 ) ) * sin( radians( latitude ) ) ) ) AS distance FROM ukpostcodes HAVING distance <= 5 ORDER BY distance
) AS tbl
I have a table of items which each have latitude and longitude values to allow me to calculate distances.
I'm trying the following but it returns: Unknown column distance in where clause:
select ( 3959 * acos( cos( radians('53.993252') )
* cos( radians( latitude ) )
* cos( radians( longitude )
- radians('-0.432470') )
+ sin( radians('53.993252') )
* sin( radians( latitude ) ) ) ) AS distance from items where distance < 1000
select ( 3959 * acos( cos( radians('53.993252') )
* cos( radians( latitude ) )
* cos( radians( longitude )
- radians('-0.432470') )
+ sin( radians('53.993252') )
* sin( radians( latitude ) ) ) ) AS distance from items having distance < 1000
WHERE doesnt see aliases use HAVING.
You can use subquery -
SELECT * FROM (
SELECT
(3959 * ACOS(COS(RADIANS('53.993252')) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - RADIANS('-0.432470')) + SIN(RADIANS('53.993252')) * SIN(RADIANS(latitude)))) AS distance
FROM items)t
WHERE distance < 1000;
I have a table called stores which contains store information and more importantly location coordinates of stores of various merchants. A merchant can have multiple stores at various locations. I need a listing of stores ordered by distance (from a particular location) with only the closest store of each merchant showing in the list.
The following query works but I believe its sub-optimal. Is there a way to revise or improve this query?
SELECT
`Store`.`id`,
`Merchants`.`id`,
`Store`.`area_id`,
`Store`.`url`,
( 6371 * acos( cos( radians(18.973212066666665) ) * cos( radians( Store.latitude ) ) * cos( radians( Store.longitude ) - radians(72.8140959) ) + sin( radians(18.973212066666665) ) * sin( radians( Store.latitude ) ) ) ) AS distance
FROM
`stores` AS `Store`
INNER JOIN
(SELECT DISTINCT id,( 6371 * acos( cos( radians(18.973212066666665) ) * cos( radians( Store.latitude ) ) * cos( radians( Store.longitude ) - radians(72.8140959) ) + sin( radians(18.973212066666665) ) * sin( radians( Store.latitude ) ) ) ) AS distance FROM stores as Store WHERE Store.active=1 AND Store.parent_id=0 AND (( 6371 * acos( cos( radians(18.973212066666665) ) * cos( radians( Store.latitude ) ) * cos( radians( Store.longitude ) - radians(72.8140959) ) + sin( radians(18.973212066666665) ) * sin( radians( Store.latitude ) ) ) ) < 50) GROUP BY Store.id ORDER BY distance) AS `St` ON (`St`.`id` = `Store`.`id`)
INNER JOIN
merchants AS `Merchants` ON (`Store`.`merchant_id` = `Merchants`.`id`)
WHERE
(( 6371 * acos( cos( radians(18.973212066666665) ) * cos( radians( `Store`.`latitude` ) ) * cos( radians( `Store`.`longitude` ) - radians(72.8140959) ) + sin( radians(18.973212066666665) ) * sin( radians( `Store`.`latitude` ) ) ) ) < 50) AND
`Store`.`parent_id` = 0 AND
`Store`.`active` = 1
GROUP BY
`Merchants`.`id`
ORDER BY
`distance` ASC
LIMIT 10
I am using the following formula to calculate distance ( 6371 * acos( cos( radians(LATITUDE) ) * cos( radians( Store.latitude ) ) * cos( radians( Store.longitude ) - radians(LONGITUDE) ) + sin( radians(LATITUDE) ) * sin( radians( Store.latitude ) ) ) )
Looks like you don't need the merchant table at all. You are using it only for merchant_id which you have in the store table anyways. So you can avoid this join.
If I understand what you need correctly, you can try this query:
select id, merchant_id, area_id, url,
min(distance function you are using) as distance
from stores group by merchant id, order by distance
I did not actually run it, but the idea should work.
-Mansi