V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
iugo
V2EX  ›  问与答

PostgreSQL 中数组与 JSON 组合的查询问题, ANY() 与 ? 的方位冲突

  •  
  •   iugo · Aug 18, 2017 · 2781 views
    This topic created in 3175 days ago, the information mentioned may be changed or developed.

    thetable 表中有一个名为 jsonb_array 的字段, 类型是 jsonb[].

    该字段有以下数据:

    [{"a": 1}]
    [{"a": 2}]
    [{"b": 1}]
    [{"b": 2}]
    

    我想要把

    [{"b": 1}]
    [{"b": 2}]
    

    取出来, 使用 SELECT * FROM thetable WHERE ANY (jsonb_array) ? 'b'; 报错.

    因为 ANY() 要求必须写在 operator 的右边. 可是 jsonb 的 ? 操作符不像 @> / <@ 一样有可以互换方向的, ? 要求筛选的内容必须写在其左边. 这可如何是好?

    1 replies    2017-08-18 18:08:53 +08:00
    iugo
        1
    iugo  
    OP
       Aug 18, 2017
    没有人回答更好的办法...

    看了官方文档, 建议使用 `generate_subscripts()` 完成.

    https://www.postgresql.org/docs/9.6/static/arrays.html
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4471 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 00:11 · PVG 08:11 · LAX 17:11 · JFK 20:11
    ♥ Do have faith in what you're doing.