zoukankan      html  css  js  c++  java
  • mysql 查询json类型数据

    先查询mysql的版本,如果mysql版本在5.7及以上版本可以使用json格式

    select version()

    如果 t1表里有一个extra字段,字段是text类型,数据为json格式 {"value":"XMjgxIqqqqqqqqqq"}

    通过jSON_EXTRACT可以获取json里面value对于的值


    JSON_EXTRACT(t1.extra,'$.value')

    得到 "XMjgxIqqqqqqqqqq"

    如果想去除两侧引号,需要先做类型转换再做trim


    trim(both '"' from cast(JSON_EXTRACT(t1.extra,'$.value') as char))

    得到 XMjgxIqqqqqqqqqq

    原始sql和原始结果

    select t1.id AS item_id, t1.title AS item_name,t6.id AS topic_id, 
           t6.title AS topic_name, t1.extra AS media_id, t1.biz_type 
            from tem t1
            join component_item t2 on t1.id = t2.item_id
            join component t3 on t2.component_id = t3.id
            join drawer t4 on t4.id = t3.drawer_id
            join channel_drawer t5 on t5.drawer_id = t4.id
            join channel t6 on t6.id = t5.channel_id
            where t1.biz_type in ("JUMP_TO_SHOW","JUMP_TO_VIDEO")
                  and t1.extra IS NOT NULL 
                  and t6.topic_property IS NOT NULL 

    优化后的sql

    select t1.id AS item_id, t1.title AS item_name,t6.id AS topic_id, 
           t6.title AS topic_name, trim(both '"' from cast(JSON_EXTRACT(t1.extra,'$.value') as char)) AS media_id, 
           trim(LEADING 'JUMP_TO_' from t1.biz_type) AS biz_type
            from item_pre t1
            join component_item_pre t2 on t1.id = t2.item_id
            join component_pre t3 on t2.component_id = t3.id
            join drawer_pre t4 on t4.id = t3.drawer_id
            join channel_drawer_pre t5 on t5.drawer_id = t4.id
            join channel_pre t6 on t6.id = t5.channel_id
            where t1.biz_type in ("JUMP_TO_SHOW","JUMP_TO_VIDEO")
                  and t1.extra IS NOT NULL 
                  and t6.topic_property IS NOT NULL
                  and JSON_EXTRACT(t6.topic_property,'$.group')= "电影"
  • 相关阅读:
    .NET Demon为Visual Studio提供持续编译和测试功能
    java面试题:字符串的排列算法
    JS实现IP地址判断
    明茨伯格管理进行时的读书感想:好书,适合有一定管理经验的人沉淀
    N个小时学MM IMG设定_存货管理和盘点 <四>
    上线第一天
    ISurfaceOp 接口生成等高线(一)
    出差合肥,路经武汉,后到长沙
    新博客
    出差合肥,路经武汉,后到长沙2
  • 原文地址:https://www.cnblogs.com/iwangzheng/p/8000207.html
Copyright © 2011-2022 走看看