查询jsonb列中的数组值

时间:2019-11-22 16:27:39

标签: sql postgresql jsonb

我的jsonb表中有一个名为chores的{​​{1}}列,其数据看起来像这样:

work

每个用户在[{"task": "feed_dog", "value": "Daily"},{"task": "mop_floor", "value": "Weekly"]数组中可能有零到几十个任务。

如何按任务名称查询?例如,拉出至少一个chorestask的所有记录。

feed_dogSELECT chores->>'task' FROM work一样返回一堆null结果。

1 个答案:

答案 0 :(得分:0)

您需要取消嵌套数组:

/Users/sambarnett/.webppl/node_modules/webppl-viz/node_modules/vega-scenegraph/src/util/bound.js:18
  return g2D || (g2D = canvas.instance(1,1).getContext('2d'));
                                           ^

TypeError: Cannot read property 'getContext' of null
    at context (/Users/sambarnett/.webppl/node_modules/webppl-viz/node_modules/vega-scenegraph/src/util/bound.js:18:44)
    at textMark (/Users/sambarnett/.webppl/node_modules/webppl-viz/node_modules/vega-scenegraph/src/util/bound.js:169:11)
    at Object.itemBounds [as item] (/Users/sambarnett/.webppl/node_modules/webppl-viz/node_modules/vega-scenegraph/src/util/bound.js:259:3)
    at /Users/sambarnett/.webppl/node_modules/webppl-viz/node_modules/vega/src/scene/Bounder.js:36:13
    at Array.forEach (<anonymous>)
    at Bounder.proto.evaluate (/Users/sambarnett/.webppl/node_modules/webppl-viz/node_modules/vega/src/scene/Bounder.js:35:15)
    at Model.prototype.evaluate (/Users/sambarnett/.webppl/node_modules/webppl-viz/node_modules/vega-dataflow/src/Graph.js:329:16)
    at Builder.proto.evaluate (/Users/sambarnett/.webppl/node_modules/webppl-viz/node_modules/vega/src/scene/Builder.js:227:26)
    at Model.prototype.evaluate (/Users/sambarnett/.webppl/node_modules/webppl-viz/node_modules/vega-dataflow/src/Graph.js:329:16)
    at Model.prototype.propagate (/Users/sambarnett/.webppl/node_modules/webppl-viz/node_modules/vega-dataflow/src/Graph.js:161:22)

使用Postgres 12可以更容易地编写它:

select w.*
from "work" w
where exists (select *
              from jsonb_array_elements(w.chores) as t(task)
              where t.task ->> 'task" = 'feed_dog');