【问题标题】:Postgres Select Where JSON Field Not NullPostgres 选择 JSON 字段不为空的位置
【发布时间】:2014-04-13 17:34:18
【问题描述】:

我有一个表,其中有一列称为警告。 2行的列值如下所示

                             warnings
-------------------------------------------------------------------
 {"isNew":false,"fieldWarnings":[],"dupId":null,"conflictIds":[]}
 {"isNew":false,"fieldWarnings":[],"dupId":3763,"conflictId":null}

我想要一个 sql 语句来选择顶行而不是底行。我尝试了这个 sql 查询,但它选择了两行

select warnings from table where cast(warnings->>'dubId' AS TEXT) is null;

【问题讨论】:

    标签: json postgresql select


    【解决方案1】:

    您的查询中有 dubId,但 JSON 属性是 dupId。我想你刚才打错了!

    试试这个:

    select warnings from table where cast(warnings->>'dupId' AS TEXT) is null;
    

    此外,->> 运算符已经将字段值作为文本返回,因此您不需要强制转换:

    select warnings from table where warnings->>'dupId' is null;
    

    【讨论】:

      猜你喜欢
      • 2016-05-01
      • 2018-07-06
      • 1970-01-01
      • 2023-03-28
      • 2012-05-07
      • 2021-03-19
      • 1970-01-01
      • 2018-08-09
      • 2018-04-30
      相关资源
      最近更新 更多