Laravel: 查询具有对象数组的JSON列

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

Laravel: query JSON column that has array of objects

问题

如何使用查询生成器或原始查询获取具有key_twovalue = 1type = static的行35

请注意,上述查询是使用JSON数据的条件进行检索的示例。您可以使用不同的数据库查询生成器或原始查询来实现此目标,具体取决于您所使用的数据库系统和编程语言。以下是一种可能的方法,供您参考:

使用 Laravel 查询生成器(如果您在使用 Laravel 框架):

$desiredRow = DB::table('your_table_name')
    ->where('id', 35)
    ->whereJsonContains('json_col', [
        ['key_two.value' => 1, 'key_two.type' => 'static']
    ])
    ->first();

在上述示例中,我们使用whereJsonContains方法来检查json_col列中的JSON数据是否包含指定的条件。

如果您使用不同的数据库系统或编程语言,请查阅相关文档以了解如何执行此类JSON条件查询。

英文:

I have a table that looks like this:

id json_col
35 [{"key_one":4,"key_two":{"value":1,"type":"static"}},{"key_one":27,"key_two":{"value":26,"type":"dynamic"}}]
36 [{"key_one":2,"key_two":{"value":33,"type":"static"}},{"key_one":9,"key_two":{"value":1,"type":"any"}}]
[
    {
        "id": 35,
        "json_col": [
            {
                "key_one": 4,
                "key_two": {
                    "value": 1,
                    "type": "static"
                }
            },
            {
                "key_one": 27,
                "key_two": {
                    "value": 26,
                    "type": "dynamic"
                }
            }
        ],
        "created_at": "2023-02-13T16:54:13.000000Z",
        "updated_at": "2023-02-13T16:54:13.000000Z"
    },
    {
        "id": 36,
        "json_col": [
            {
                "key_one": 2,
                "key_two": {
                    "value": 33,
                    "type": "static"
                }
            },
            {
                "key_one": 9,
                "key_two": {
                    "value": 1,
                    "type": "any"
                }
            }
        ],
        "created_at": "2023-02-13T16:54:56.000000Z",
        "updated_at": "2023-02-13T16:54:56.000000Z"
    }
]

How to get the row 35 that has key_two with value = 1 and type = static, using the query builder or a raw query?

答案1

得分: 1

你可以根据存储方式使用whereJsonContains来处理多维数组。

return Model::whereJsonContains('json_col', [['key_two' => ['value' => 1]]])
    ->whereJsonContains('json_col', [['key_two' => ['type' => 'static']]])
    ->paginate(10); // 或者使用 get()

仅需仔细检查SQL输出是否确实与你的JSON格式相似,应该如下所示:

WHERE json_contains(`json_col`, '[{"key_two":{"value":1}}]')
AND json_contains(`json_col`, '[{"key_two":{"type":"static"}}]')

编辑

如果你需要在单个对象中搜索多个匹配项,可以使用以下方法:

return Model::whereJsonContains('json_col', [['key_two' => ['value' => 1, 'type' => 'static']])
    ->paginate(10); // 或者使用 get()
英文:

you can use whereJsonContains with multi-dimentional array base on how you store them.

return Model::whereJsonContains('json_col', [ ['key_two' => [ 'value' => 1] ] ])
->whereJsonContains('json_col', [ ['key_two' => [ 'type' => 'static'] ] ])
->paginate(10); // or get()

just double check the sql output actually looks like your json format which should look something like

WHERE json_contains(`json_col`, '[{\"key_two\":{\"value\":1}}]')
AND json_contains(`json_col`, '[{\"key_two\":{\"type\":\"static\"}}]')

EDIT

If you need to search multiple match in single object, then this should do

return Model::whereJsonContains('json_col', [ ['key_two' => [ 'value' => 1, 'type' => 'static' ] ] ])
->paginate(10); // or get()

huangapple
  • 本文由 发表于 2023年2月14日 21:01:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448218.html
匿名

发表评论

匿名网友

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

确定