[Bast-commits] r6187 -
DBIx-Class/0.08/branches/count_distinct/lib/DBIx/Class/Manual
arcanez at dev.catalyst.perl.org
arcanez at dev.catalyst.perl.org
Sat May 9 01:50:12 GMT 2009
Author: arcanez
Date: 2009-05-09 01:50:12 +0000 (Sat, 09 May 2009)
New Revision: 6187
Modified:
DBIx-Class/0.08/branches/count_distinct/lib/DBIx/Class/Manual/Cookbook.pod
Log:
rewrite DISTINCT/COUNT(DISTINCT) Cookbook entries
Modified: DBIx-Class/0.08/branches/count_distinct/lib/DBIx/Class/Manual/Cookbook.pod
===================================================================
--- DBIx-Class/0.08/branches/count_distinct/lib/DBIx/Class/Manual/Cookbook.pod 2009-05-08 21:11:43 UTC (rev 6186)
+++ DBIx-Class/0.08/branches/count_distinct/lib/DBIx/Class/Manual/Cookbook.pod 2009-05-09 01:50:12 UTC (rev 6187)
@@ -237,30 +237,50 @@
=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 $count = $rs->next->get_column('count');
+ my $rs = $schema->resultset('Artist')->search(
+ {},
+ {
+ columns => [ qw/name/ ],
+ group_by => [ qw/name/ ],
+ }
+ );
+ my $count = $rs->count;
+
+ # Equivalent SQL:
+ # SELECT COUNT( DISTINCT( me.name ) ) FROM artist me
+
=head2 Grouping results
L<DBIx::Class> supports C<GROUP BY> as follows:
More information about the Bast-commits
mailing list