2013-09-30 68 views
1

我試圖按照這個線程SQL Limit results per column value來顯示每個分支只有前10位的數量,但我在Perl中編寫查詢時遇到了麻煩。mysql查詢在perl和mysql中執行的結果不同

如果我在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的所有行。我建議如果內部的操作不起作用,但我不知道爲什麼和如何。所有幫助,將不勝感激

回答

2

在你準備幫助之前,這是否把這個?

$db->do('set @num=-1'); 
$db->do('set @mainbranch=-1'); 
+0

謝謝ysth。它很棒! – user2020692