[Bast-commits] r5523 -
DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Manual
robkinyon at dev.catalyst.perl.org
robkinyon at dev.catalyst.perl.org
Wed Feb 18 19:58:21 GMT 2009
Author: robkinyon
Date: 2009-02-18 19:58:21 +0000 (Wed, 18 Feb 2009)
New Revision: 5523
Modified:
DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Manual/Cookbook.pod
Log:
Added some more POD in the Cookbook for correlated subqueries
Modified: DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Manual/Cookbook.pod
===================================================================
--- DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Manual/Cookbook.pod 2009-02-18 19:54:31 UTC (rev 5522)
+++ DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Manual/Cookbook.pod 2009-02-18 19:58:21 UTC (rev 5523)
@@ -303,7 +303,7 @@
name => [ 'Billy Joel', 'Brittany Spears' ],
});
- my $rs = $schema->resulset('CD')->search({
+ my $rs = $schema->resultset('CD')->search({
artist_id => { 'IN' => $inside_rs->get_column('id')->as_query },
});
@@ -312,10 +312,32 @@
B<NOTE>: You have to explicitly use '=' when doing an equality comparison.
The following will B<not> work:
- my $rs = $schema->resulset('CD')->search({
+ my $rs = $schema->resultset('CD')->search({
artist_id => $inside_rs->get_column('id')->as_query,
});
+=head3 Correlated subqueries
+
+ my $cdrs = $schema->resultset('CD');
+ my $rs = $cdrs->search({
+ year => {
+ '=' => $cdrs->search(
+ { artistid => { '=' => \'me.artistid' } },
+ { alias => 'inner' }
+ )->get_column('year')->max_rs->as_query,
+ },
+ });
+
+That creates the following SQL:
+
+ SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
+ FROM cd me
+ WHERE year = (
+ SELECT MAX(inner.year)
+ FROM cd inner
+ WHERE artistid = me.artistid
+ )
+
=head2 Predefined searches
You can write your own L<DBIx::Class::ResultSet> class by inheriting from it
More information about the Bast-commits
mailing list