[Dbix-class] How to use a function on multiple fields?

Jess Robinson castaway at desert-island.demon.co.uk
Wed Jan 10 09:24:00 GMT 2007



On Wed, 10 Jan 2007, Karl.Moens at marsh.com wrote:

> Dear list-members and DBIx::Class developers,
>
> Can any of you enlighten me on the best way --if at all possible in
> DBIx::Class-- to "translate" the following SQLquery in a DBIx::Class
> structure?
>
> SELECT STD(field1 + field2) FROM table WHERE field1 + field2 > 10
>
> [STD is of course the standard deviation function, which is part of the
> MySQL function set]
>
> As you will know, STD(field1) + STD(field2) <> STD(field  + field2),
> otherwise it would be too easy to apply the function to each field
> separately and then add them in my Perl script.
>
> For the condition "field1 + field2 > 10", I have now coded it in raw SQL,
> but I wonder if there isn't a more DBIC-ish way.
>

Hi CountZero! ;)

I'm fairly sure you can just do this:

->search({ .. }, { select => [ { 'std' => [1, 2]} ] ..

of course, since those are fields, and you don't want them quoted, you 
have to resort to:

->search({ .. }, { select => [ \'STD(field1 + field2)' ] ..

which is a bit less pretty.. (we need scalarrefs=literal everywhere!

Jess





More information about the Dbix-class mailing list