如何使用COUNT,SUM,CASE和BETWEEN将此SQL查询转换为Perl DBIx :: Class?

时间:2011-01-19 16:42:51

标签: sql perl between dbix-class

如何将此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 个答案:

答案 0 :(得分:1)

假设您的意思是DBIx :: Class,那么我可能会选择以下内容:

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();

当然,日期时间位将取决于您的日期时间格式,是否自动充气/缩小日期时间等