mysql中json_contains和json_search的

2021-12-30  本文已影响0人  PENG先森_晓宇

共同点

差异

json_contains

json_contains参数需要指定path,path如果不存在返回Null,path存在但是指定值不存在返回0,指定值存在则返回1。该函数需要注意的地方请查看该文章

mysql> select json_contains(remarks,'"xiaoyu"','$.name') from order_remarks;
+--------------------------------------------+
| json_contains(remarks,'"xiaoyu"','$.name') |
+--------------------------------------------+
| NULL                                       |
| NULL                                       |
| NULL                                       |
|                                          1 |
|                                          1 |
|                                          1 |
| NULL                                       |
| NULL                                       |
|                                          0 |
+--------------------------------------------+

json_search

json_search(json_doc,one_or_all,search_str [,escape_char [, path]])
和json_contains相比,该函数不需要指定具体的path,更像是like一样的模糊查询。
如果查询的str存在,则返回具体的path,如果不存在则返回null。

mysql> select json_search(remarks,'one','xiaoyu') from order_remarks;
+-------------------------------------+
| json_search(remarks,'one','xiaoyu') |
+-------------------------------------+
| NULL                                |
| NULL                                |
| NULL                                |
| "$.name"                            |
| "$.name"                            |
| "$.name"                            |
| NULL                                |
| NULL                                |
| NULL                                |
+-------------------------------------+

注意
json_search()只能搜索字符串,而不能搜索整形

比如 str字段值为{"name":"xiaoyu","old":12}

mysql> select json_search(str,'one',12) from order_remarks;
+-------------------------------------+
| json_search(remarks,'one','xiaoyu') |
+-------------------------------------+
| NULL                                |
| NULL                                |
| NULL                                |
| NULL                                |
| NULL                                |
| NULL                                |
| NULL                                |
| NULL                                |
| NULL                                |
+-------------------------------------+

总结

json_contains适合用于指定path的查询,而json_search适合模糊查询,比如说看下json中是否有'hello'这个字符串,而并不需要知道它在什么位置。

上一篇 下一篇

猜你喜欢

热点阅读