基本上我有以下存储过程:
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
它有以下传递:
IN rid varchar(500), lat double, lon double
但我需要传递纬度和经度变量。我已经尝试设置它们然后将它们添加到查询中但它没有识别它们。这就是我想做的不成功的事情:
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
我不知道如何做到这一点。有没有人有什么建议?提前谢谢!!
答案 0 :(得分:3)
您可以将?
占位符放在预准备语句中,然后在执行时提供值。
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;
答案 1 :(得分:1)
Barmar的答案将是我首选的解决方案,但举一个例子来说明我最初的评论建议......
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