在mysql选择查询中传递perl数组

在mysql选择查询中传递perl数组,mysql,arrays,perl,select,Mysql,Arrays,Perl,Select,我的数组包含多个整数。数组是@group\u id。假设它包含三个元素,如12、45、87。我想将它们传递给select语句,如下所示 select * from groups where id in (@group_id) // should get all the ids inside it. 目前,我无法获取查询中的值。您可以使用 local $" = ","; 在查询之前,如果id是数字,但这会使您容易受到攻击,请使用带有?占位符的查询 my $placeholders = join

我的数组包含多个整数。数组是
@group\u id
。假设它包含三个元素,如12、45、87。我想将它们传递给select语句,如下所示

select * from groups where id in (@group_id) // should get all the ids inside it.

目前,我无法获取查询中的值。

您可以使用

local $" = ",";
在查询之前,如果id是数字,但这会使您容易受到攻击,请使用带有
占位符的查询

my $placeholders = join ",", ("?") x @group_id;
my $sql = "select * from groups where id in ($placeholders)";

# $sth prepare..
$sth->execute(@group_id);

你可以用像这样的东西

local $" = ",";
在查询之前,如果id是数字,但这会使您容易受到攻击,请使用带有
占位符的查询

my $placeholders = join ",", ("?") x @group_id;
my $sql = "select * from groups where id in ($placeholders)";

# $sth prepare..
$sth->execute(@group_id);

查询将查找字符串值,例如
'1,2,3'
,而不是三个单独的值

如果在字符串中构造查询,可以直接在字符串中插入值:

where id in (".@group_id.") . . 
但是,您需要小心,因为这会使您面临SQL注入攻击

如果您的表不是很大(或者您不关心性能),可以使用
find\u in\u set()


查询将查找字符串值,例如
'1,2,3'
,而不是三个单独的值

如果在字符串中构造查询,可以直接在字符串中插入值:

where id in (".@group_id.") . . 
但是,您需要小心,因为这会使您面临SQL注入攻击

如果您的表不是很大(或者您不关心性能),可以使用
find\u in\u set()


在打印选择查询时,我没有在“in(@group_id)”中获得任何值。在打印选择查询时,我没有在“in(@group_id)”中获得任何值。库戈登·林诺夫:)谢谢戈登·林诺夫:)