1 个查询中的 Perl DBI 2 个动态数组
Perl DBI 2 dynamic arrays in 1 query
我目前有一个包含 2 个多选框的网页,其中 returns 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);
行不通。
您的方法会奏效。
您可以将任意数量的数组传递给一个函数。数组只是值的列表。
考虑以下示例:
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)
时,它只会将每个数组评估为一个列表。您可以根据需要组合任意数量的列表,它们将始终扁平化。
我目前有一个包含 2 个多选框的网页,其中 returns 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);
行不通。
您的方法会奏效。
您可以将任意数量的数组传递给一个函数。数组只是值的列表。
考虑以下示例:
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)
时,它只会将每个数组评估为一个列表。您可以根据需要组合任意数量的列表,它们将始终扁平化。