[Dbix-class] Filtering a SELECT based on a calculation of two columns?

Rob Kinyon rob.kinyon at gmail.com
Thu Feb 17 15:17:20 GMT 2011


On Thu, Feb 17, 2011 at 10:01, Chris Cole <chris at compbio.dundee.ac.uk> wrote:
> Hi all,
>
> I've got a table where two of the columns (say a and b) are simple integers
> and I want to select rows where b - a < 100. How do I go about that in DBIx.
>
> The equivalent SQL would be:
> SELECT * FROM table WHERE b - a < 100;

You have a few possibilities:

1) The easiest solution is $rs->search{ \'b - a < 100' );
2) b - a < 100 is equivalent to b < a + 100. So, that would be
$rs->search({ b => { '<' => \'a + 100' } });

But, in both cases, you might want to parameterize the 100, so:
$rs->search(\[ 'b - a < ?', { dummy => 100 } ]);
$rs->search({ b => { '<' => \[ 'a + ?', { dummy => 100 } ] };

Rob



More information about the DBIx-Class mailing list