From: Justin Hunter Date: Sat, 9 May 2009 01:50:12 +0000 (+0000) Subject: rewrite DISTINCT/COUNT(DISTINCT) Cookbook entries X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=58a201072aab5b683f2e5faa956c4a63c9d0f2b7;p=dbsrgits%2FDBIx-Class-Historic.git rewrite DISTINCT/COUNT(DISTINCT) Cookbook entries --- diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index 7d0de14..1fb791b 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -237,29 +237,49 @@ any of your aliases using either of these: =head2 SELECT DISTINCT with multiple columns - my $rs = $schema->resultset('Foo')->search( + my $rs = $schema->resultset('Artist')->search( {}, { - select => [ - { distinct => [ $source->columns ] } - ], - as => [ $source->columns ] # remember 'as' is not the same as SQL AS :-) + columns => [ qw/artistid name rank/ ], + distinct => 1 + } + ); + + my $rs = $schema->resultset('Artist')->search( + {}, + { + columns => [ qw/artistid name rank/ ], + group_by => [ qw/artistid name rank/ ], } ); + # Equivalent SQL: + # SELECT me.artistid, me.name, me.rank + # FROM artist me + # GROUP BY artistid, name, rank + =head2 SELECT COUNT(DISTINCT colname) - my $rs = $schema->resultset('Foo')->search( + my $rs = $schema->resultset('Artist')->search( {}, { - select => [ - { count => { distinct => 'colname' } } - ], - as => [ 'count' ] + columns => [ qw/name/ ], + distinct => 1 + } + ); + + my $rs = $schema->resultset('Artist')->search( + {}, + { + columns => [ qw/name/ ], + group_by => [ qw/name/ ], } ); - my $count = $rs->next->get_column('count'); + my $count = $rs->count; + + # Equivalent SQL: + # SELECT COUNT( DISTINCT( me.name ) ) FROM artist me =head2 Grouping results