From: Rob Kinyon Date: Wed, 18 Feb 2009 19:58:21 +0000 (+0000) Subject: Added some more POD in the Cookbook for correlated subqueries X-Git-Tag: v0.08240~63^2~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=68a797c15d885198e73d9bb218101e202d0d9b79;p=dbsrgits%2FDBIx-Class.git Added some more POD in the Cookbook for correlated subqueries --- diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index 9838759..600d326 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -303,7 +303,7 @@ You can write subqueries relatively easily in DBIC. 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 @@ The usual operators ( =, !=, IN, NOT IN, etc) are supported. B: You have to explicitly use '=' when doing an equality comparison. The following will B 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 class by inheriting from it