Postgresql jsonb连续选择数组的不同元素(具有相同的键)

时间:2018-10-29 14:35:12

标签: postgresql jsonb

使用此jsonb:

[
    {
        "#text": "ANNALS OF THE RHEUMATIC DISEASES",
        "@type": "source"
    },
    {
        "#text": "ANN RHEUM DIS",
        "@type": "source_abbrev"
    },
    {
        "#text": "Ann. Rheum. Dis.",
        "@type": "abbrev_iso"
    },
    {
        "#text": "ANN RHEUM D",
        "@type": "abbrev_11"
    },
    {
        "#text": "ANN RHEUM DIS",
        "@type": "abbrev_29"
    },
    {
        "#text": "Comparison of initial versus delayed introduction of a treat-to-target strategy in patients with recent-onset rheumatoid arthritis: results of the T-4 3-year study",
        "@type": "item"
    }
]

我可以这样选择:

select b.x->>'#text' from b 
where b.x @> '{"@type" : "source"}'::jsonb

但是我要选择的是例如#text,其中的键是一行中的“ item”,“ abbrev_iso”和“ source”。可以用普通的SQL完成此操作,还是编写一个函数来帮助查询更好呢?

结果必须看起来像这样:

Source                                 Abbrev                Item
ANNALS OF THE RHEUMATIC DISEASES       Ann. Rheum. Dis.      Comparison of initial versus....

1 个答案:

答案 0 :(得分:0)

以下代码有效。初步测试表明,在10000条记录上,它比将3个传统表连接到相同结果要慢大约8倍。

... OR(<condition1>, <condition2>)