In MySQL 5.7, how to use JOIN on JSON fields to implement associative queries?
1. Existing table Theme_Table1 , where the field Theme_table2_ids is the JSON type, the value is an array, and the values in the array are associated with the primary key ID of the table Theme_Table2 . as shown in Figure 1
2. I have written to query whether the ID in a theme_table2 exists in the table theme_table1 before. SQL implementation is as follows
select * from `theme_table1` where json_contains(`theme_table2_ids`, '391') limit 1
3. Now you need to query which tables the records in theme_table2 exist in the field Theme_table2_ids of the table Theme_table1. The sql implementation is as follows, as shown in Figure 2
SELECT
`theme_table1`.theme_table2_ids,
`theme_table2`.id,
`theme_table2`.processing,
`theme_table2`.processing_failed
FROM
`theme_table1`
INNER JOIN `theme_table2` ON JSON_CONTAINS(
`theme_table1`.theme_table2_ids,
CONVERT ( `theme_table2`.id, CHAR )) = 1
WHERE
`processing` = 0
AND `processing_failed` = 1

