【问题标题】:How to search in mysql table where field in json array field?如何在mysql表中搜索json数组字段中的字段?
【发布时间】:2020-10-04 06:00:13
【问题描述】:

我有一个带有 JSON 数组字段的表, 我想要一个基于此字段从另一个表返回结果的查询

table_a

+----+-------------+
| id | interestIds |
+----+-------------+
|  1 | [1, 2, 6, 7]|
+----+-------------+

table_b

+----+-------------+
| id | country     |
+----+-------------+
|  1 | Ghana       |
|  2 | Grenada     |
|  3 | Jordan      |
|  4 | Latvia      |
|  5 | Malawi      |
|  6 | Mexico      |
|  7 | Moldova     |
+----+-------------+

我想要像这样的查询

select * from table_b where id in (select interestIds from table_a where id = 1)

【问题讨论】:

    标签: mysql sql arrays join select


    【解决方案1】:

    你可以使用json_search():

    select b.*
    from table_b
    where exists (
        select 1 from table_a a where json_search(a.interestIds, 'one', b.id) is not null
    )
    

    【讨论】:

      猜你喜欢
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2018-04-01
      相关资源
      最近更新 更多