2013-08-05 16 views

答えて

6

DB[:some_table].select{coalesce(some_column, 0)}

1

代替構文のバージョンはSequel.function(:coalesce,:some_column,5)です。

例:

require 'sequel' 
Sequel.extension :pretty_table  
DB = Sequel.sqlite 
DB.create_table(:mytables){ 
    primary_key :id 
    field :a, :type => :nvarchar, :size => 10 
    field :some_column, :type => :fixnum 
} 

DB[:mytables].insert(:a => 'a1', :some_column => 10) 
DB[:mytables].insert(:a => 'a2') 

Sequel::PrettyTable.print(
    DB[:mytables].select(:a, :some_column, Sequel.function(:coalesce,:some_column,5)) 
) 

結果:

+--+--------------------------+-----------+ 
|a |coalesce(`some_column`, 5)|some_column| 
+--+--------------------------+-----------+ 
|a1|      10|   10| 
|a2|       5|   | 
+--+--------------------------+-----------+ 
関連する問題