SQL查询中的对象数组筛选条件

huangapple go评论69阅读模式
英文:

SQL Query Where array of objects

问题

我有一个包含对象数组的字段的表格。这是字段的一个示例值:

[
   {a: "test"},
   {a: "testing"}
]

我试图查询数据,其中比较是在数组对象字段上进行的,类似于这样的查询:SELECT * FROM table_1 WHERE table_1.a = "test"。我无法理解的部分是在WHERE条件上,或者这是否不可能实现?

英文:

I have a table that has a field that contains an array of objects. Here's a sample value of the field

[
   {a: "test"},
   {a: "testing"}
]

I'm trying to do a query data where the comparison is on the array of object fields that kinda looks like this SELECT * FROM table_1 WHERE table_1.a = "test". The part that I can't get is on the WHERE condition or is this not possible?

答案1

得分: 0

我已找到答案

要么是这两者之一

SELECT * FROM table_1 WHERE table_1.field @>'[{"a":"test"}]'::jsonb

SELECT * FROM table_1 WHERE table_1.field::text 类似于 '%("a": "test")%'
英文:

I have found an answer to this

its either of this two

   SELECT * FROM table_1 WHERE table_1.field@>'[{"a":"test"}]'::jsonb

or

   SELECT * FROM table_1 WHERE table_1.field::text similar to '%("a": "test")%'

huangapple
  • 本文由 发表于 2023年1月6日 11:36:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定