- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- ソース を表示
- Perl-DBIC/SELECT 基本 へ行く。
- 1 (2007-05-02 (水) 03:52:20)
- 2 (2007-09-04 (火) 01:45:48)
- 3 (2007-12-11 (火) 02:17:23)
- 4 (2008-04-08 (火) 03:53:28)
- 5 (2008-04-16 (水) 13:55:33)
- 6 (2008-04-22 (火) 19:47:29)
- 7 (2008-04-24 (木) 17:30:24)
- 8 (2008-05-26 (月) 05:55:13)
- 9 (2008-06-13 (金) 06:02:45)
- 10 (2008-06-16 (月) 03:00:36)
- 11 (2008-09-05 (金) 09:34:34)
- 12 (2008-10-23 (木) 09:52:45)
- 13 (2009-07-09 (木) 11:46:42)
- 14 (2009-11-14 (土) 07:35:54)
- 15 (2009-11-20 (金) 12:39:06)
- 16 (2009-12-16 (水) 07:56:55)
- 17 (2010-02-16 (火) 16:24:27)
- 18 (2010-04-09 (金) 14:47:04)
- 19 (2010-05-17 (月) 06:11:30)
- 20 (2010-05-18 (火) 16:55:53)
- 21 (2010-06-07 (月) 05:50:36)
- 22 (2011-02-28 (月) 02:23:37)
- 23 (2011-05-17 (火) 12:04:15)
- 24 (2011-06-16 (木) 11:21:18)
- 25 (2011-06-30 (木) 01:38:33)
- 26 (2011-11-09 (水) 06:59:28)
SELECT
http://search.cpan.org/perldoc?DBIx::Class::ResultSet#search
SEARCH
$rs->search({address => 'tokyo'},{order_by => ['name', 'id DESC']}) $rs->search([{address => 'tokyo'},{address => 'osaka'}]) # "tokyo" OR "osaka" $rs->search({age => {'>=' => 20}}) $rs->search({hobby => undef}) # SQL:"hobby is null"
DBの関数を比較(の対象)に使う
$rs->search({"date_part('year', age(now(), birth_date))" => { '<=' => 18 } }); # 年齢が18歳以下 # date_part()とage()はPostgreSQLの関数
DBの関数を比較(の値)に使う
不等号「>」も含めて、スカラリファレンスにする。
$rs->search( { start_day => \' > now()' } );
FIND
プライマリキーで検索する。見つからないときは、undefが返ってくる。
my $cd = $schema->resultset('CD')->find(5);
LIKE
$rs = $rs->search_like({title => '%blue%'});
IN
$rs->search({user_id => {-in => \@user_ids}})
OR
$rs->search([{user.address => '東京'},{user.address => '大阪'}], {join => 'user'});
特定のカラムのみSELECT
$rs->search({},{ select => [ 'name', 'age', ], as => [ 'name', '年齢' ], }); print $r->name; print $r->get_column('年齢');
GROUP BY (AS)
$rs->search({},{ select => ['name',{sum=>'point'}], as => ['name','point'], group_by => ['name'], });
$point = $rs->next->get_column('point');
JOIN (3つのテーブル)
SELECT order.* FROM order JOIN order_detail ON order.id = order_detail.order_id JOIN goods order_detail.goods_id = goods.id WHERE goods.name = 'book'
$rs->search({ "goods.name" => 'book', },{ join => {order_detail => 'goods'} });
自己結合
SELECT XXX FROM category cate1 JOIN category cate2 ON cate1.parent_category_id = cate2.category_id
$rs = $rs->search( {}, { from => [ { cate1 => 'category' }, [ { cate2 => 'category' }, { 'cate1.parent_category_id' => 'cate2.category_id' } ] ] } );
PREFETCH
$rs->search(undef,{prefetch => 'user', join => 'user'});
ジョインするテーブルをjoin=>'TABLE名'で明示する事。
COUNT()
$rs = $rs->search(); print $rs->count;
↓SELECT COUNT(DISTINCT name) FROM user
$rs = $rs->seach({},{ select => [ { count => { distinct => 'name' } } ], as => ['count'], }); print $rs->next->get_column('count');
LIMIT / OFFSET
my $rs = $schema->resultset('CD')->search({ title => 'TEST' },{ order_by => 'id' })->slice(0,20);
slice($offset,$limit)を使う。