2011-08-24 84 views
3

我有一個名爲x.txt與以下數據的文本文件:的Perl - DBI :: CSV排序問題

emailid,hits 
aa,100 
bb,200 
cc,300 
dd,400 
ee,500 
aa,400 

和我的Perl代碼

use DBI; 
$dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_dir => ".",csv_sep_char  => "," }); 

my $query = "SELECT emailid,sum(hits) tothits FROM x.txt group by emailid order by tothits desc"; 
my $sth = $dbh->prepare ($query); 
$sth->execute(); 
while (my $row = $sth->fetchrow_hashref) { 
    print $row->{emailid},"--",$row->{tothits},"\n"; 

    } 
$sth->finish(); 

返回

cc--300 
bb--200 
dd--400 
aa--500 
ee--700 

這裏怎麼回事?

+1

你也有「ee,200」嗎? – Tim

回答

1

你的SQL看起來沒問題。這種排序沒有發生的原因似乎是因爲limitations in DBD::CSV

我試圖看看它是否可以與DBD :: AnyData一起使用,但不能識別tothits並死亡。

看起來你需要做一個解決方法,比如fetchall然後排序。

+0

在SQL :: Statement中,限制(省略?,錯誤?)更可能。 – bohica