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

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

Laravel: query JSON column that has array of objects

问题

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

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

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

  1. $desiredRow = DB::table('your_table_name')
  2. ->where('id', 35)
  3. ->whereJsonContains('json_col', [
  4. ['key_two.value' => 1, 'key_two.type' => 'static']
  5. ])
  6. ->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"}}]
  1. [
  2. {
  3. "id": 35,
  4. "json_col": [
  5. {
  6. "key_one": 4,
  7. "key_two": {
  8. "value": 1,
  9. "type": "static"
  10. }
  11. },
  12. {
  13. "key_one": 27,
  14. "key_two": {
  15. "value": 26,
  16. "type": "dynamic"
  17. }
  18. }
  19. ],
  20. "created_at": "2023-02-13T16:54:13.000000Z",
  21. "updated_at": "2023-02-13T16:54:13.000000Z"
  22. },
  23. {
  24. "id": 36,
  25. "json_col": [
  26. {
  27. "key_one": 2,
  28. "key_two": {
  29. "value": 33,
  30. "type": "static"
  31. }
  32. },
  33. {
  34. "key_one": 9,
  35. "key_two": {
  36. "value": 1,
  37. "type": "any"
  38. }
  39. }
  40. ],
  41. "created_at": "2023-02-13T16:54:56.000000Z",
  42. "updated_at": "2023-02-13T16:54:56.000000Z"
  43. }
  44. ]

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来处理多维数组。

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

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

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

编辑

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

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

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

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

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

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

EDIT

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

  1. return Model::whereJsonContains('json_col', [ ['key_two' => [ 'value' => 1, 'type' => 'static' ] ] ])
  2. ->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:

确定