Basically I have the following stored procedure:
BEGIN
SET @query := CONCAT("SELECT *,
sqrt(
(POW(a.Latitude - co.CenterLatitude, 2)* 68.1 * 68.1) +
(POW(a.Longitude - co.CenterLongitude, 2) * 53.1 * 53.1)
) AS distance
FROM table1 as r
JOIN table2 as co ON co.field1 = r.field2
JOIN table3 AS a ON r.field1 = a.field2
WHERE ",rid);
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
It has the following passing through:
IN rid varchar(500), lat double, lon double
But I need to pass in the latitude and longitude variables. I have tried setting them then adding them into the query but it is not recognizing them. This is what I am trying to do which is not successful:
BEGIN
SET @lat := lat;
SET @lon := lon;
SET @query := CONCAT("SELECT *,
sqrt(
(POW(a.Latitude - @lat, 2)* 68.1 * 68.1) +
(POW(a.Longitude - @lon, 2) * 53.1 * 53.1)
) AS distance
FROM table1 as r
JOIN table2 as co ON co.field1 = r.field2
JOIN table3 AS a ON r.field1 = a.field2
WHERE ",rid);
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
I am not sure how to accomplish this. Does anyone have any suggestions? Thank you in advance!!
You can put ?
placeholders in the prepared statement, and then supply the values when executing it.
SET @query := CONCAT("SELECT *,
sqrt(
(POW(a.Latitude - ?, 2)* 68.1 * 68.1) +
(POW(a.Longitude - ?, 2) * 53.1 * 53.1)
) AS distance
FROM table1 as r
JOIN table2 as co ON co.field1 = r.field2
JOIN table3 AS a ON r.field1 = a.field2
WHERE ",rid);
PREPARE stmt FROM @query;
EXECUTE stmt USING @lat, @lon;
Barmar's answer would be my preferred solution, but to give an example of my what my initial comment was suggesting...
BEGIN
SET @query := CONCAT("SELECT *,
sqrt(
(POW(a.Latitude - ", lat, ", 2)* 68.1 * 68.1) +
(POW(a.Longitude - ", lon, ", 2) * 53.1 * 53.1)
) AS distance
FROM table1 as r
JOIN table2 as co ON co.field1 = r.field2
JOIN table3 AS a ON r.field1 = a.field2
WHERE ",rid);
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.