【问题标题】:Postgres Aggregate HstorePostgres 聚合 Hstore
【发布时间】:2014-10-25 02:28:04
【问题描述】:

我是 Postgres 的新手,我正在开发一个现有的 Laravel 应用程序。条目表使用名为datahstore 列。这是有问题的查询:

$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)

我不知道为什么,谁能解释一下这个问题?

【问题讨论】:

    标签: php sql postgresql laravel eloquent


    【解决方案1】:

    使用NULLIF 处理空字符串:

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

    如果所有字段都为NULL,也可以使总和为NULL。

    【讨论】:

    • 我会接受这一点,因为这实际上解决了我的问题。非常感谢
    【解决方案2】:

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

    问题是“总”条目是空的,当转换为小数时会导致错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多