[Dbix-class] Newbie from SQL to DBIx

Matthew Somerville matthew at mysociety.org
Thu Mar 8 11:56:46 GMT 2018


On 8 March 2018 at 06:52, Andreas Dafferner
<andreas.dafferner at adw.uni-heidelberg.de> wrote:
> select distinct left (col, (length(col) - 9)) from table order by col

You could do this:

FixMyStreet::DB->resultset("Table")->search({}, {
    columns => { col => \"left(col, length(col)-9)" },
    distinct => 1,
    order_by => "left(col, length(col)-9)"  });

Run with DBIC_TRACE, the SQL for this is:
    SELECT left(col, length(col)-9) FROM table me GROUP BY left(col,
length(col)-9) ORDER BY left(col, length(col)-9):

And the resultant column is available as ->col for each row.

ATB,
Matthew



More information about the DBIx-Class mailing list