postgres中的jsonb查询

时间:2018-03-08 01:30:57

标签: arrays postgresql jsonb

我在postgres中有一个名为op_user_event_data的表,它有一个名为data的列,我存储了一个jsonb,我现在拥有的是这样的json:

{
    "aisles": [],
    "taskGroups": [
        {
            "index": 0,
            "tasks": [
                {
                    "index": 1,
                    "mandatory": false,
                    "name": "Dados de Linear",
                    "structuresType": null,
                    "lines": [
                        {
                            "sku": {
                                "skuId": 1,
                                "skuName": "Limiano Bola",
                                "marketId": [
                                    1,
                                    3,
                                    10,
                                    17
                                ],
                                "productId": 15,
                                "brandId": [
                                    38,
                                    44
                                ]
                            },
                            "taskLineFields": [
                                {
                                    "tcv": {
                                        "value": "2126474"
                                    },
                                    "columnType": "skuLocalCode",
                                    "columnId": 99
                                },
                                {
                                    "tcv": {
                                        "value": null
                                    },
                                    "columnType": "face",
                                    "columnId": 29
                                },
                            ]
                        },
                        {
                            "sku": {
                                "skuId": 3,
                                "skuName": "Limiano Bolinha",
                                "marketId": [
                                    1,
                                    3,
                                    10,
                                    17
                                ],
                                "productId": 15,
                                "brandId": [
                                    38,
                                    44
                                ]
                            },
                            "taskLineFields": [
                                {
                                    "tcv": {
                                        "value": "2545842"
                                    },
                                    "columnType": "skuLocalCode",
                                    "columnId": 99
                                },
                                {
                                    "tcv": {
                                        "value": null
                                    },
                                    "columnType": "face",
                                    "columnId": 29
                                },
                            ]
                        },
                        {
                            "sku": {
                                "skuId": 5,
                                "skuName": "Limiano Bola 1/2",
                                "marketId": [
                                    1,
                                    3,
                                    10,
                                    17
                                ],
                                "productId": 15,
                                "brandId": [
                                    38,
                                    44
                                ]
                            },
                            "taskLineFields": [
                                {
                                    "tcv": {
                                        "value": "5127450"
                                    },
                                    "columnType": "skuLocalCode",
                                    "columnId": 99
                                },
                                {
                                    "tcv": {
                                        "value": "5.89"
                                    },
                                    "columnType": "rsp",
                                    "columnId": 33
                                }
                            ]
                        }

基本上我有一个对象 过道[], taskGroups, id和name。

在json中显示的taskGroups中,其中一个属性是一个数组的任务,它还有一个名为lines的数组,其中包含一系列sku和任务行。

基本上:

taskGroups - >任务 - >线 - > sku或taskLineFields。

我尝试过不同的查询来获取sku但是当我尝试获得比“行”更多的东西时,它只是空白或者在其他尝试中“无法调用标量中的元素”

任何人都可以帮我解决这个问题吗?请注意,这只是一个示例json。

任何人都知道如何使其发挥作用:

我想要所有行 - > taskLineFields-> columnType ='offer'

我能做的就是这个,但是在标量上抛出错误:

    SELECT lines->'sku' Produto, lines->'taskLineFields'->'tcv'->>'value' ValorOferta
FROM sd_bel.op_user_event_data,
     jsonb_array_elements(data->'taskGroups') taskgroups,
     jsonb_array_elements(taskgroups->'tasks') tasks,
     jsonb_array_elements(tasks->'columns') columns,
     jsonb_array_elements(tasks->'lines') lines
WHERE created_by = 'belteste'
AND  lines->'taskLineFields'->>'columnType' = 'offer'

1 个答案:

答案 0 :(得分:0)

说明您的table

中的数据位于某个with t as ( select json_column as xyz from table ), tg as ( select json_array_elements(xyz->'taskGroups') taskgroups from t), tsk as (select json_array_elements(taskgroups->'tasks') tasks from tg) select json_array_elements(tasks->'lines') -> 'sku' as sku from tsk;
<?php

// load
require_once 'vendor/autoload.php';

// want to load the file path
$source = __DIR__ . '/helloWorld.docx';

// load a DOCX file
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'Word2007');         # Word2007

// output a docx file
$phpWord->save('test_out.docx');