Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join JSON array field with string field in mysql

Tags:

json

sql

mysql

I am looking for records in table 2 whose id exist in the values of nums field (JSON) in table 1.

table1

id | nums (JSON)
---+-------------------
1  | ["1","2","3","4"]
2  | ["7","8","5","6"]
3  | ["9","10","3","4"]

table2

id |
---+
1  | 
2  | 
53 | 
63 | 

I would like to get the next result.

rows desired

id |
---+
1  | 
2  | 

I am using 5.7 mysql version.

like image 483
J. Ordaz Avatar asked May 23 '26 22:05

J. Ordaz


1 Answers

Try this,

SELECT * FROM table1 as t1
    LEFT JOIN table 2 as t2 on JSON_CONTAINS(t1.id->'$[*]', CAST(t2.id as JSON))
like image 155
Crysis Avatar answered May 25 '26 11:05

Crysis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!