MySql存储过程,如何插入多个变量与CONCAT。

时间:2022-01-23 16:41:23

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!!

我不知道该怎么做。有人有什么建议吗?提前谢谢你! !

2 个解决方案

#1


3  

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;

#2


1  

Barmar's answer would be my preferred solution, but to give an example of my what my initial comment was suggesting...

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

#1


3  

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;

#2


1  

Barmar's answer would be my preferred solution, but to give an example of my what my initial comment was suggesting...

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