2011-01-19 93 views
1

如何將此SQL查詢轉換爲Perl DBIx::Class代碼?如何使用COUNT,SUM,CASE和BETWEEN將此SQL查詢轉換爲Perl DBIx :: Class?

SELECT COUNT(*) AS num_grads, 
     SUM(CASE WHEN employment_status = 1 THEN 1 ELSE 0 END) AS num_employed 
    FROM students 
WHERE status = 6 -- 6 means they graduated 
    AND grad_date BETWEEN Convert(datetime, ?) AND Convert(datetime, ?) 
+1

你的意思是DBIx :: Class? – 2011-01-19 16:53:11

+0

是的,DBIx :: Class。 – Justin 2011-01-19 21:48:23

回答

1

假設你的意思DBIx ::類,那麼我可能會去的東西,如:

my $rs = My::Schema->resultset('Students') # 
    ->search({ 
     status => 6, 
     grad_date => { 'between' => [ $start_dt, $end_dt ] }, 
    }); 

my $num_grads = $rs->count(); 
my $num_employed = $rs->search({ employment_status => 1 })->count(); 

當然的日期時間位將取決於你的日期時間格式,無論你是自動充氣/縮減日期時間等