2014-08-31 49 views
0

我是Postgres的新手,我正在研究现有的Laravel应用程序。条目表使用名为datahstore列。这是有问题的查询:Postgres Aggregate Hstore

$query = \DB::table('entries') 
     ->where('f_id', '=', $f->id) 
     ->where('s_id', '=', \Config::get('s_id')) 
     ->select(\DB::raw('SUM(CAST("data"->\'total\' as decimal)) as "total"')) 
     ->get(); 

我可以看到他们在这里做什么,但这段代码失败,出现错误:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type numeric: \"\" (SQL: select SUM(CAST(\"data\"->'total' as decimal)) as \"total\" from \"entries\" where \"f_id\" = 70 and \"s_id\" = 1) 

我想不通为什么,任何人都可以解释问题?

回答

2

使用NULLIF采取空字符串的护理:

'SUM(NULLIF("data"->\'total\', '')::numeric)) as "total"' 

可以使之NULL为好,如果所有字段是NULL。

+0

我打算接受这个,因为这实际上解决了我的问题。非常感谢 – outrunthewolf 2014-09-01 10:33:32

0

这里的问题不是实际的查询。

问题是“总计”条目是空的,当输入到小数点时会导致错误。

相关问题