php – 如何在mysql中搜索JSON数据?

前端之家收集整理的这篇文章主要介绍了php – 如何在mysql中搜索JSON数据?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我用json_encoded将数据插入数据库.
现在我想搜索功能”,但我不能.

MySQL查询

SELECT  `id`,`attribs_json` 
FROM  `products` 
WHERE  `attribs_json` REGEXP  '"1":{"value":[^"3"$]'

这个查询显示了所有的行与键“1”,值是任何东西,不是值“3”

我的资料是:

{"feature":{"1":{"value":"["2","3"]"},"2":{"value":["1"]},"5":{"value":""},"3":{"value":["1"]},"9":{"value":""},"4":{"value":"\u0633\u0627\u062a\u0646"},"6":{"value":""},"7":{"value":""},"8":{"value":""}
           },"show_counter":"0","show_counter_discount":""
}}
如果你有MysqL版本> = 5.7,那么你可以试试这个 –
SELECT  JSON_EXTRACT(name,"$.id") as name
 FROM table
 WHERE JSON_EXTRACT(name,"$.id") > 3

输出

+-------------------------------+
| name                          | 
+-------------------------------+
| {"id": "4","name": "Betty"}  | 
+-------------------------------+

请查看参考链接了解更多详情https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html

猜你在找的PHP相关文章