There is no problem not worth solving, and no technology not worth learning!

In MySQL 5.7, how to use JOIN on JSON fields to implement associative queries?

现有表 theme_table1 ,其中字段 theme_table2_ids 为 json 类型,值为数组,数组中的值关联了表 theme_table2 的主键ID

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

现有表 theme_table1 ,其中字段 theme_table2_ids 为 json 类型,值为数组,数组中的值关联了表 theme_table2 的主键ID
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

现在需要查询出有哪些表 theme_table2 中的记录存在于 表 theme_table1 的 字段 theme_table2_ids 中。SQL 实现如下
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


Need long-term technical maintenance or remote troubleshooting?

I am a PHP / Go backend engineer with 15+ years of experience, focused on existing system maintenance, bug fixing, performance optimization, server troubleshooting, WordPress maintenance, and small feature iterations.

If your project is facing any of the following issues, we can start with a small troubleshooting task first:

  • ✅ PHP / Laravel / Yii2 legacy systems without active maintenance
  • ✅ Go / Gin backend APIs that need troubleshooting or optimization
  • ✅ Slow, broken, or unstable WordPress websites
  • ✅ Nginx / MySQL / Redis / Linux server issues
  • ✅ CDN / Cloudflare / DNS / HTTPS configuration problems
  • ✅ Long-term remote technical support or part-time maintenance

More details: About Me & Collaboration

WeChat: 13980074657
Email: shuijingwanwq@gmail.com
Telegram: @shuijingwan
GitHub: https://github.com/shuijingwan

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.