mysql查询json类型字段提取需要类

2022/6/14 2:20:09

本文主要是介绍mysql查询json类型字段提取需要类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

如商品订单信息表

查询需要信息并做模糊处理查询:

select
t.goods_order_id orderId,
t.goods_id goodsId,
t.goods_spe_id speId,
t.goods_order_no orderNumber,
t.number orderGoodsNum,
JSON_EXTRACT(t.buy_customer,'$.nickName') as nickName,
JSON_EXTRACT(t.receive_customer,'$.name') as receiver,
JSON_EXTRACT(t.receive_customer,'$.phone') as mobile,
JSON_EXTRACT(t.receive_customer,'$.area[0].text') as addressOne,
JSON_EXTRACT(t.receive_customer,'$.area[1].text') as addressTwo,
JSON_EXTRACT(t.receive_customer,'$.area[2].text') as addressThree,
JSON_EXTRACT(t.receive_customer,'$.area[3].text') as addressFour,
JSON_EXTRACT(t.receive_customer,'$.address') as address,
JSON_EXTRACT(t.goods_shot_cut,'$.goods.name') as goodName,
JSON_EXTRACT(t.goods_shot_cut,'$.spe.speName') as speName,
JSON_EXTRACT(t.logistics_info,'$.logisticsCompany') as logisticName,
JSON_EXTRACT(t.logistics_info,'$.logisticsNo') as expressNumber,
DATE_FORMAT(t.creation_time,'%Y-%m-%d %H:%i:%s') createTime
from merchant_db.t_mall_goods_order t
where t.store_id = #{storeId}
and t.status != 0
and t.status != -10
<if test="orderStatus!=null and orderStatus!='' and orderStatus!= 0 and orderStatus!= -10 ">
    and t.status = #{orderStatus}
</if>
<if test="mobile!=null and mobile!=''">
    and JSON_EXTRACT(t.receive_customer, '$."phone"') like CONCAT('%',#{mobile},'%')
</if>
<if test="receiver!=null and receiver!=''">
    and JSON_EXTRACT(t.receive_customer, '$."name"') like CONCAT('%',#{receiver},'%')
</if>
<if test="userName!=null and userName!=''">
    and JSON_EXTRACT(t.buy_customer, '$."userName"') like CONCAT('%',#{userName},'%')
</if>
<if test="nickName!=null and nickName!=''">
    and JSON_EXTRACT(t.buy_customer, '$."nickName"') like CONCAT('%',#{nickName},'%')
</if>
<if test="commodityName!=null and commodityName!=''">
    and JSON_EXTRACT(t.goods_shot_cut, '$.goods."name"') like CONCAT('%',#{commodityName},'%')
</if>
<if test="orderNumber!=null and orderNumber!=''">
    and t.goods_order_no like CONCAT('%',#{orderNumber},'%')
</if>
<if test=" startTime!=null and startTime!= ''">
    and t.creation_time >= #{startTime}
</if>
<if test=" endTime != null and endTime != ''">
    and DATE_ADD(str_to_date(#{endTime}, '%Y-%m-%d'),INTERVAL 1 DAY) > t.creation_time
</if>
order by t.creation_time desc


mysql 取json字符串字段下的某个键的值

要求:mysql版本5.7及以上

由于json的键值是带双引号。所以需要去掉双引号。1.使用replace()做替换

select 
    replace(JSON_EXTRACT(infoJson,'$.uid'),'"','') uid
from users

或
JSON.parse(orderResp.getExpressNumber()).toString() 去掉 "" 号
 
 


这篇关于mysql查询json类型字段提取需要类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程