我尝试遵循此线程SQL 限制每列值的结果,以根据数量仅显示每个分支的前 10 个,但我在用 Perl 编写查询时遇到了麻烦。
如果我在 MYSQL Front 中运行查询,它会根据数量产生每个分支的前 10 名。但是当我将查询放入带有 DBI 模块的 Perl 中时,它会产生所有行。我的 Perl 脚本有什么问题吗?
Mysql 查询:
select name, branch, amount from (select name, branch, amount, if(@mainbranch = branch, @num := @num + 1, (@num := 0 || @mainbranch := branch)) as num from branch_amount where branch in (102, 106, 0 ) order by branch, amount desc ) a where num<=10
Perl 脚本:
use DBI;
my $db=DBI->connect("DBI:mysql:$database;host=$host;user=$user;password=$passwd")
my $query = "select name, branch, amount from (select name, branch, amount, if(\@mainbranch = branch, \@num := \@num + 1, (\@num := 0 || \@mainbranch := branch)) as num from branch_amount where branch in (102, 106, 0 ) order by branch, amount desc ) a where num<=10";
$db->prepare("$query");
$sth->execute() or die "$query";
我尝试从 Perl 脚本中计算结果,所有行的 num 字段值都是 1。我建议 if 内的操作不起作用,但我不知道为什么以及如何。所有帮助将不胜感激