给定表 t
中的 JSON 对象类型列,例如
| id | obj |
| -- | ---------------------------------- |
| 1 | { "params": { "id": [13, 23]} } |
| 2 | { "params": { "id": [13, 24]} } |
| 3 | { "params": { "id": [11, 23, 45]} }|
和一个数值列表,例如[12、23、45]
。
我们需要检查每条记录是否包含给定列表中的值。 所以,期望的结果是
| id | matches |
| -- | -------- |
| 1 | [23] |
| 3 | [23, 45] |
有人可以帮忙查询 MySQL 8 吗? 谢谢!
最佳答案
你可以使用json_table
:
select t2.id, t2.n_obj from (
select t1.id, (select json_arrayagg(ids.v)
from json_table(t1.obj, "$.params.id[*]" columns(v text path '$')) ids
where json_contains('[12, 23, 45]', ids.v, '$'))
n_obj from t t1) t2
where t2.n_obj is not null;
关于mysql - 在 MySQL 的 JSON 数组字段中查找匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70957415/