传递DBI时出现perl错误->;执行IN子句中的值
我有一个查询,用于根据此处的文档计算位于某个点的某个半径内的对象: 它工作得很好,但是我只想搜索那些特定类型的对象,这会导致一个问题 代码如下所示:传递DBI时出现perl错误->;执行IN子句中的值,perl,dbi,Perl,Dbi,我有一个查询,用于根据此处的文档计算位于某个点的某个半径内的对象: 它工作得很好,但是我只想搜索那些特定类型的对象,这会导致一个问题 代码如下所示: my $sql = "SELECT * FROM ( SELECT b.*, pr.postcode, pr.prize, pr.title, pr.collection, pr.redeemed, pr.delivery, pr.archived, bt.category, p.radius, p.distan
my $sql = "SELECT *
FROM (
SELECT b.*, pr.postcode, pr.prize, pr.title, pr.collection, pr.redeemed, pr.delivery, pr.archived, bt.category,
p.radius,
p.distance_unit
* DEGREES(ACOS(COS(RADIANS(p.latpoint))
* COS(RADIANS(b.lat))
* COS(RADIANS(p.longpoint - b.lng))
+ SIN(RADIANS(p.latpoint))
* SIN(RADIANS(b.lat)))) AS distance
FROM bubbles AS b, bubble_prizes AS pr, bubble_types AS bt
JOIN ( /* these are the query parameters */
SELECT ? AS latpoint, ? AS longpoint,
? AS radius, ? AS distance_unit
) AS p
WHERE b.lat
BETWEEN p.latpoint - (p.radius / p.distance_unit)
AND p.latpoint + (p.radius / p.distance_unit)
AND b.lng
BETWEEN p.longpoint - (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint))))
AND p.longpoint + (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint))))
AND pr.bubble = b.id
AND b.type IN ?
AND b.type = bt.type
) AS d
WHERE distance <= radius
ORDER BY distance";
其中“(type1,type2)”应传递给
b.type IN ?
(位于SQL语句的底部附近)
我已经尝试了所有我能想到的方法来摆脱这个字符串,这样它就可以工作了(包括很多明显疯狂但我绝望的方法)
等等(我试了很多东西,都记不清了。)
无论我尝试什么,我都会得到一个SQL错误
DBD::mysql::st execute failed: 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 ''(type1, type2)'
AND b.type = bt.type
) AS d
WHERE distance <= radius'
DBD::mysql::st execute失败:您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在“”附近使用的正确语法(type1,type2)
b.type=bt.type
)AS d
如果距离,则需要在(…)
语句中的中使用占位符。execute()。您可以创建动态占位符列表,如下所示:
my @types = qw(type1 type2);
my $placeholders = join ", ", ("?") x @types;
my $sql = "...
b.typeID IN ($placeholders)
...";
my $points = $y->dbh->prepare($sql);
$results = $points->execute($lat, $lng, $rad, $units, @types);
DBD::mysql::st execute failed: 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 ''(type1, type2)'
AND b.type = bt.type
) AS d
WHERE distance <= radius'
my @types = qw(type1 type2);
my $placeholders = join ", ", ("?") x @types;
my $sql = "...
b.typeID IN ($placeholders)
...";
my $points = $y->dbh->prepare($sql);
$results = $points->execute($lat, $lng, $rad, $units, @types);