- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- ソース を表示
- 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
where句を設定する
$rs->search({ age => 20 }) # WHERE age = 20 = $rs->search({ age => {'>' => 20} }) # WHERE age > 20 > $rs->search({ age => undef }) # WHERE age is NULL NULL $rs->search([{ age => 20 },{ age => 30 }]) # WHERE age = 20 OR age = 30 OR $rs->search({ age => 20, name => 'taro }) # WHERE age = 20 AND name = 'taro' AND $rs->search({ 'age' => { '>' => 10, '<' => 20 }}) # WHERE age > 10 AND age < 20 AND 同じカラムを使う場合 $rs->search_like({ name => '%taro%' }) # WHERE name LIKE '%taro%' LIKE $rs->search({id => {-in => \@ids}}) # WHERE id in (1,2,3..) IN
http://search.cpan.org/perldoc?DBIx::Class::ResultSet#search
結果を取り出す
順次取り出す
my $rs = $schema->resultset('User')->search({ age => 20}); while (my $user = $rs->next) { }
1行だけ取り出す
my $user = $schema->resultset('User')->single({ age => 20});
最初の行を取り出す
my $user = $schema->resultset('User')->search({ age => 20})->first;
全行取り出す
my @users = $schema->resultset('User')->search({ age => 20})->all;
http://search.cpan.org/perldoc?DBIx::Class::ResultSet
プライマリキーで結果を取り出す
見つからないときは、undefが返ってくる。
my $cd = $schema->resultset('CD')->find(5);
ORDER BY
$rs->search({},{ order_by => [ 'name', { -desc => 'id' } ] # ORDER BY name ASC, id DESC }); # 'name' は { -asc => 'name' } の省略形
http://search.cpan.org/perldoc?SQL::Abstract#ORDER_BY_CLAUSES
NULLを0と見なしてORDER BYする方法
(多分)DBICではCASE式が使えないので、COALESCE関数を使う。例えばpointカラムを降順でソートするには以下のようにする。
$rs->search(undef, { order_by => [ { '-desc' => \'COALESCE(point, 0)' } ], });
GROUP BY
$rs->search({},{ select => ['name', {SUM => 'point'}], as => ['name', 'point'], group_by => ['name'], }); $point = $rs->next->get_column('point');
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
$rs->search({},{ order_by => 'id' })->slice(0,20); # slice($offset,$limit)
特定のカラムのみSELECT(AS演算子)
$rs->search({},{ select => [ 'name', 'age' ], as => [ 'name', '年齢'], }); print $r->name, $r->get_column('年齢');
DB関数
sum()
# SELECT sum(point) AS total_point FROM t1; $rs = $rs->search( {}, { select => [ { sum => 'point' } ], as => ['total_point'], } ); my $total_point = $rs->first ? $rs->first->get_column('total_point') : 0;
DBの関数を比較の対象に使う
$rs->search({"date_part('year', age(now(), birth_date))" => { '<=' => 18 } }); # 年齢が18歳以下 # date_part()とage()はPostgreSQLの関数
DBの関数を比較の値に使う
不等号「>」も含めて、スカラリファレンスにする。
$rs->search( { start_day => \' > now()' } );