6

在 SQL 中,为了避免获得 NULL 值,我可以使用“coalesce”函数来替换它,如下所示:

SELECT COALESCE(some_column, 0) FROM some_table;  

但是我找不到任何方法来使用 Sequel 做同样的事情。

4

2 回答 2

8

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

于 2013-08-05T18:59:38.677 回答
2

另一种语法版本是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|           |
+--+--------------------------+-----------+
于 2015-08-19T13:51:41.330 回答