简体   繁体   中英

How to search in mysql table where field in json array field?

I have a table with JSON array field, I want a query that returns results from another table based on this field

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     |
+----+-------------+

i want query like

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

You can use 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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM