【问题标题】:Use json_populate_recordset in a case-insensitive way以不区分大小写的方式使用 json_populate_recordset
【发布时间】:2016-12-05 03:03:59
【问题描述】:

是否可以使用json_populate_recordset 以便使用 PostgreSQL (9.6) 以不区分大小写的方式比较表列名/json 键?

例如,下面的 sn-p 将返回零行。

CREATE TABLE foo (bar TEXT);
SELECT * from json_populate_recordset(null::foo, '[{"bAr":1}]')

当然,我可以将 json 键转换为小写,或者表名可以区分大小写。

【问题讨论】:

    标签: json postgresql


    【解决方案1】:

    我认为不区分大小写是不可能的。如果您事先知道将用于记录的大小写(例如,它们总是驼峰式),您可以通过引用列名来指定特定大小写。

    显示不区分大小写的基线示例:

    # create type x as (abc integer);
    CREATE TYPE
    # select * from json_populate_recordset(null::x, '[{"abc" : 1}, {"Abc" : 2}, {"aBc" : 3}, {"abC" : 4}]');
     abc
    -----
       1
    
    
    
    (4 rows)
    

    现在让我们通过引用列名来选择我们想要使用的特定情况。

    # drop type x;
    DROP TYPE
    # create type x as ("aBc" integer);
    CREATE TYPE
    edgar=# select * from json_populate_recordset(null::x, '[{"abc" : 1}, {"Abc" : 2}, {"aBc" : 3}, {"abC" : 4}]');
     aBc
    -----
    
    
       3
    
    (4 rows)
    

    如果您不能保证输入数据的大小写,则将所有内容显示为小写。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 2011-03-08
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      相关资源
      最近更新 更多