how to call MySql Stored procedure multiple time with multiple parameter. - mysql

CREATE PROCEDURE NearBy(IN VarLat VARCHAR(20), IN VarLng VARCHAR(20))
BEGIN
SELECT CustId,address, ( 6371 * acos( cos( radians(VarLat) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(VarLng) ) + sin( radians(VarLat) ) * sin( radians( Latitude ) ) ) ) AS distance FROM ordermaster HAVING distance < 0.02 ORDER BY distance LIMIT 0 , 10;
END;
call NearBy('21.1021258','79.0659928')
this my procedure to find nearby customer. parameter are lat and lng.
latitude and longitude are stored in database table.
i want result like no of order are coming form particular area of radius of 20 meter.

Related

MySQL - Find points within radius from database

I have a table which has a POINT column containing the latitude and longitude of various locations.
I then also have a users location from geo-location in the browser.
What I need to be able to do is find all records from the table where the POINT value in the is within a 10 km radius (or X km radius), ordered by distance with the closest first.
My table has a SPATIAL index on the POINT column.
I'm currently working on a project where I'm calculating distances between multiple locations. I'm using the following query for selecting object_id's which are within a given radius.
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_radius ORDER BY distance ASC"
I can't explain the ACOS formula itself because I got it from research.
db_latitude = database latitude field
db_longitude = database longitude field
$user_latitude = browser latitude coördinate
$user_longitude = browser longitude coördinate
$the_radius = the radius that you want to search in
This is in kilometers.
The query below actually worked for me :
$query = "SELECT *,
( 6371 *
acos(
cos( radians( ".$user_lat." ) ) *
cos( radians( lat ) ) *
cos( radians( lng ) -
radians( ".$user_lng." ) ) +
sin( radians( ".$user_lat." ) ) *
sin( radians( lat ) ) ) )
AS distance FROM parkings
HAVING distance <= ".$radius." ORDER BY distance ASC";
$stmt = $conn->execute($query);
$rows = $stmt->fetchAll('assoc');
where:
$user_lat and $user_lng is browser's lat and lng,
$radius = 10,
table name is parkings
May be this help for you,
https://ru.scribd.com/presentation/2569355/Geo-Distance-Search-with-MySQL
For Django I use this
dist = 20 #дистанция 20 км
mylon = 51.5289156201 # долгота центра
mylat = 46.0209384922 # широта
lon1 = mylon-dist/abs(math.cos(math.radians(mylat))*111.0) # 1 градус широты = 111 км
lon2 = mylon+dist/abs(math.cos(math.radians(mylat))*111.0)
lat1 = mylat-(dist/111.0)
lat2 = mylat+(dist/111.0)
profiles = UserProfile.objects.filter(lat__range=(lat1, lat2)).filter(lon__range=(lon1, lon2))
It search all users in squar 20km.

Find by latitude and longitude in mysql

i want to search near by location with given latitude and longitude in mysql
latitude is : 26.902
longitude is : 75.793
and distance is : 30
Query is :
SELECT
id, (
3959 * acos (
cos ( radians(26.902) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(75.793) )
+ sin ( radians(26.902) )
* sin( radians( latitude ) )
)
) AS distance
FROM business
HAVING distance < 30
ORDER BY distance
LIMIT 0 , 20;
Result:
i am getting a record with distance is 3.58,
record lat/long are 26.89 / 75.74
but when i check online on other site i got distance 5.759 miles .
Some code available here as a Stored Function: http://mysql.rjweb.org/doc.php/latlng .

Virtual column unknown when comparing lat lng

I am attempting to return only rows where the latitude and longitude being passed into the query, when compared to the latitude and longitude stored in the database, is a certain amount of miles apart.
The query is as follows:
SELECT
c.google_theatre_id
AS cinema_id,
c.name
AS cinema_name,
( 3959 * acos( cos( radians('50.4521013') ) *
cos( radians( latitude ) ) *
cos( radians( longitude ) -
radians('-3.5247389') ) +
sin( radians('50.4521013') ) *
sin( radians( latitude ) ) ) )
AS distance
FROM
google_cinemas c, app_users u
WHERE
distance < u.range
AND
u.id = 126
ORDER BY
distance
The query is designed to get the distance and then compare it to a column (range) in the app_users table.
When running the query, I'm getting an error of distance being an unknown column.
As this is a virtual column, is there a different way of comparing?
Thanks :)
you need to use HAVING instead of WHERE.. think of it this way WHERE is when you make an order at a restraunt and HAVING is picking stuff off of the plate when it comes to your table... you cannot reference an alias before the plate comes to your table only after it has been built
SELECT
c.google_theatre_id AS cinema_id,
c.name AS cinema_name,
( 3959 * acos( cos( radians('50.4521013') ) *
cos( radians( latitude ) ) *
cos( radians( longitude ) -
radians('-3.5247389') ) +
sin( radians('50.4521013') ) *
sin( radians( latitude ) ) )
) AS distance
FROM google_cinemas c, app_users u
WHERE u.id = 126
HAVING distance < u.range
ORDER BY distance
alternatively you can use it as a sub query which could be faster since HAVING re-evaluates the entire query.
SELECT *
FROM
( SELECT
c.google_theatre_id AS cinema_id,
c.name AS cinema_name,
( 3959 * acos( cos( radians('50.4521013') ) *
cos( radians( latitude ) ) *
cos( radians( longitude ) -
radians('-3.5247389') ) +
sin( radians('50.4521013') ) *
sin( radians( latitude ) ) )
) AS distance,
u.range
FROM google_cinemas c, app_users u
WHERE u.id = 126
ORDER BY distance
)t
WHERE distance < range

MySQL location select query returns 0 in stored procedure

Can anybody spot the problem here?
I have a stored procedure I wanna use to find closest locations to a specific location.
When I try the select outside of a stored proc (just in a query window) all is fine.
SELECT id,
( 3959*acos(
cos(radians(37)) *
cos(radians(lat)) *
cos(radians(lng) - radians(-122)) +
sin(radians(37)) *
sin(radians(lat))
)
) AS distance
FROM markers
HAVING distance < 25
ORDER BY distance
LIMIT 0 , 20;
But when I place it in a stored proc it always return 0.
DELIMITER $$
CREATE PROCEDURE `GetLocationsByRadius`(IN latitude double, IN longitude double,IN radius double)
begin
SELECT ( 6371 * acos( cos( radians(latitude) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(longitude ) ) + sin( radians(latitude) ) * sin( radians( Latitude ) ) ) ) AS distance FROM LocationTrades HAVING distance < radius ORDER BY distance LIMIT 0 , 20;
end $$
I tried changing the table data types from float4 to float8 to decimals, I tried using inner variables inside the stored, but nothing helps, it always returns distance 0.
Seems like it thinks something is INT inside there...
Any help would be appreciated.
Thanks
Ok, I found the problem.
The table columns need table identifiers before them. (Table.Column)
This works:
DELIMITER $$
CREATE PROCEDURE GetLocationsByRadius(IN latitude double, IN
longitude double,IN radius double) begin
SELECT ( 6371 * acos( cos( radians(latitude) ) * cos( radians(
Locations.Latitude ) ) * cos( radians( Locations.Longitude ) -
radians(longitude) ) + sin( radians(latitude) ) * sin( radians(
Locations.Latitude ) ) ) ) AS distance FROM Locations HAVING distance
< radius ORDER BY distance LIMIT 0 , 20;
end $$

SQL location from a known point query

I have a MySQL database with the following table
int - id
float 2,6 - long
float 2,6 - lat
int - radius
I want to create a SQL query which returns the ID & Distance from a given location(long & lat)
I found the following piece of code which works:
SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) *
cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) *
sin( radians( lat ) ) ) ) AS distance FROM markers
HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;
I want to alter this query to return only the rows where the computed length is smaller the radius (a column i my table)
replacing the 25 with the radius doesn't work.
Is there a way to achieve that without using two SQL queries ?
problem is occured because the result distance is in float and radius is an integer datatype so you need to CAST radius as float. try below
SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) *
cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) *
sin( radians( lat ) ) ) ) AS distance FROM markers
HAVING distance < CAST (radius AS float) ORDER BY distance LIMIT 0 , 20;
HAPPY TO HELP :)