Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

mysql json是一个id数组,怎么查找包含指定id的记录?

表中有orders字段是JSON类型,存储这类似 [1, 2, 3]这样的id数组
给定一个数组搜索条件[1, 2]
怎么查出包含数组条件任意一项值的记录?

SELECT * FROM trade WHERE orders LIKE "%1%" OR orders LIKE "%2%";

像上面这个语句一样,但这是通过字符串搜的,效率也非常低下,想请问是否有效率高点的,JSON查询的方式?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

JSON_CONTAINS 的第二个参数必须是字符串。

SELECT * FROM `trade` where JSON_CONTAINS(orders, '1','$')

图片.png

图片.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...