代码之家  ›  专栏  ›  技术社区  ›  rmuller

postgresql 10中Json值的最大长度

  •  0
  • rmuller  · 技术社区  · 8 年前

    假设我有以下数据

    WITH test(id, data) AS (
      VALUES
        (1, '{"key1": "Some text"}'::jsonb),
        (2, '{"other_key": "Some longer text"}'::jsonb),
        (3, '{"key_3": "Short"}'::jsonb)
    )
    select ??? from test;
    

    请注意,JSON数据是简单的键值数据。键可以是任何东西,值始终是字符串。

    我想返回值字段的最大字符数。16在这种情况下, select length('Some longer text') ;

    1 回复  |  直到 8 年前
        1
  •  3
  •   a_horse_with_no_name    8 年前

    您需要将这些值转换为一个集合,然后才能对其进行操作:

    WITH test(id, data) AS (
      VALUES
        (1, '{"key1": "Some text"}'::jsonb),
        (2, '{"other_key": "Some longer text"}'::jsonb),
        (3, '{"key_3": "Short"}'::jsonb)
    )
    select max(length(t.val))
    from test, jsonb_each_text(data) as t(k,val);