Home > Database > Mysql Tutorial > How to remove json field in mysql

How to remove json field in mysql

WBOY
Release: 2023-05-28 13:24:44
forward
4137 people have browsed it

    Mysql tips for retrieving json fields

    Sometimes some information is stored in the database in json form. If it is too long, sql will be used during the retrieval process. The operation will be slower, and it will be more wasteful if you only get certain key values.

    Use the function JSON_EXTRACT() in mysql

    ±—±---------------------------------------------------------------+
    | id | data |
    ±—±---------------------------------------------------------------+
    | 1 | {“Tel”: “132223232444”, “name”: “david”, “address”: “Beijing”} |
    | 2 | {“Tel”: “13390989765”, “name”: “Mike”, “address”: “Guangzhou”} |
    ±—±---------------------------------------------------------------+
    Copy after login
    select json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.tel");
    Copy after login

    Use get_json_object() in spark sql

    select get_json_object(i.extra, '$.sale_currency');
    select sum(get_json_object(i.extra, '$.sale_price') * i.count) as sale_price;
    Copy after login

    Mysql takes json. If the key is not known, get its value details

    { "selects" :{ "20071090" :{ "NN" : 41,
    "LXFS1" : "12365",
    "GH" : "20071090",
    "RZZW" : "办公室主任",
    "sxzym" : "园林植物与观赏园艺",
    "ZC" : "副教授",
    "XGW" : "行政管理后勤",
    "XB" : "男",
    "ZZMM" : "中共党员",
    "ZWZC" : "高级职称",
    "MZ" : "汉",
    "CSRQ" : 307123200000,
    "XL" : "硕士",
    "selectKey" : "20071090",
    "XM" : "张三",
    "GZBM" : "办公室",
    "PZGW" : "副教授三级岗位",
    "XW" : "农学硕士" }}}
    Copy after login

    If you want to get a name in this mysql database, the content is as shown in the picture above

    How to remove json field in mysql

    Create a view and take the name of this field. Since its key: "20071090" is dynamic and uncertain, and will only appear once in this place, first I take the first layer key==>selects The value

    SELECT ZDLSXM->'$.selects' from VI_YXGR
    Copy after login

    How to remove json field in mysql

    On this basis, take down another layer. Since this layer is a dynamic key, the first step ZDLSXM->'$ cannot be used. selects' method, so I chose the substring_index method to remove the braces, and then used CONCAT() to splice it into a new json. Delete the red area of ​​the icon below

    How to remove json field in mysql##

     SELECT CONCAT("{",substring_index(substring_index(ZDLSXM->'$.selects', '{', -1),"}",1),"}") from VI_YXGR
    Copy after login

    How to remove json field in mysql

    How to remove json field in mysql

    You can use this name by wrapping it one more layer

    SELECT SE_ZDLSXM->'$.name' AS ZDLSXM from (
    SELECT CONCAT("{",substring_index(substring_index(ZDLSXM->'$.selects', '{', -1),"}",1),"}") as SE_ZDLSXM  from VI_YXGR
    ) sss
    Copy after login

    How to remove json field in mysql

    The above is the detailed content of How to remove json field in mysql. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template