1个查询中的Perl DBI 2动态数组

时间:2016-10-13 14:29:23

标签: perl dbi

我目前有一个带有2个多选框的网页,它返回2个不同的字符串,将在我的SQL查询中使用。

我目前在查询中只使用了1个字符串,但希望添加另一个字符串,并且不确定从这里开始。

我将字符串创建为数组

@sitetemp = split ',', $siteselect;
my $params = join ', ' => ('?') x @sitetemp;

我可以使用$params

的查询
$mysql_inquire = "SELECT starttime, SUM(duration) FROM DB WHERE feature = \"$key\"  and starttime >= $start and starttime <= $end  and site IN ($params) group by starttime order by starttime";


 $sth = $DBH->prepare($mysql_inquire);
 $sth->execute(@sitetemp);

基本上我的问题是如何使用2个不同的数组做同样的事情?

我认为行$sth->execute(@sitetemp, @otherarray);不起作用。

1 个答案:

答案 0 :(得分:3)

您的方法可行。

您可以根据需要将任意数量的数组传递到函数中。数组只是值列表。

考虑以下示例:

sub foo {
    print Dumper \@_;
}
my @a = ( 1, 2, 3 );
my @b = ( 4, 5, 6 );
foo( @a, @b, ( 7, (8), 9, ( ( (10) ) ) ) );

这将打印:

$VAR1 = [
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9,
          10
        ];

当您说foo(@a, @b)时,它只会将每个数组评估为一个列表。您可以根据需要组合任意数量的列表,它们将始终展平