PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

Mysql内储存JSON字符串实例分析

WBOY
WBOY 转载
2023-06-02 19:40:26 1117浏览

    前言

    json 可以将 javascript 对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松地传递这个字符串,或者在异步应用程序中将字符串从 web 客户机传递给服务器端程序。这个字符串可以表示数组和复杂的对象,而不仅仅是键和值的简单列表,在mysql中存储json字符串可以极大的简便存储的复杂度,而与此同时,读取数据库也就成了很多人首先遇到的问题。

    示例:{ “key”: “value” }

    一、Json是什么?

    一种轻量级的数据交换格式是JSON(JavaScript Object Notation)。JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。

    二、不同情况

    1.模糊查询json类型字段

    存储的数据格式(字段名 people_json):

    {“name”: “zhangsan”, “age”: “13”, “gender”: “男”}

    代码如下(示例):

    select * from table_name  where people_json->'$.name' like '%zhang%'

    2.精确查询json类型字段

    存储的数据格式(字段名 people_json):

    {“name”: “zhangsan”, “age”: “13”, “gender”: “男”}

    代码如下(示例):

    select * from table_name  where people_json-> '$.age' = 13

    3.模糊查询JsonArray类型字段

    存储的数据格式(字段名 people_json):

    [{“name”: “zhangsan”, “age”: “13”, “gender”: “男”}]

    代码如下(示例):

    select * from table_name  where people_json->'$[*].name' like '%zhang%'

    4.精确查询JsonArray类型字段

    存储的数据格式(字段名 people_json):

    [{“name”: “zhangsan”, “age”: “13”, “gender”: “男”}]

    代码如下(示例):

    select * from table_name  where JSON_CONTAINS(people_json,JSON_OBJECT('age', "13"))
    声明:本文转载于:亿速云,如有侵犯,请联系admin@php.cn删除