【问题标题】:Using set-returning functions in WHERE clauses在 WHERE 子句中使用集合返回函数
【发布时间】:2020-02-23 00:04:09
【问题描述】:
SELECT * from users where server_id=$1 and $2=ANY(json_object_keys(contacts));

ERROR:  set-returning functions are not allowed in WHERE

contacts 是一个json 列,我需要在其contacts' 键中找到具有$2 的行。

联系人列示例: {"90144": 0, "89915": 0}

如果$290144,则查询应返回在其contacts' 键中包含90144 的每一行,如示例中所示。我怎么能做到这一点?提前致谢。

【问题讨论】:

  • SELECT true WHERE '90144' IN ( SELECT json_object_keys('{"90144": 0, "89915": 0}'::json)) -- 是的

标签: postgresql set-returning-functions


【解决方案1】:

使用? operator

SELECT * from users where server_id=$1 and contacts::jsonb ? $2;

【讨论】:

  • 这行得通。谢谢你。但我很好奇为什么强制转换为 jsonb 。不是吗?使用 json?
  • 不,那个只适用于 JSONB。您可能应该将列类型更改为 JSONB,因为这种类型的操作要快得多——它以已经解析的形式存储。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 2017-11-12
  • 1970-01-01
相关资源
最近更新 更多