赞
踩
本次查询是在未知如何分库分表的情况下,对表数据进行查询,其中有的字段为JSON结构。需要提取JSON中某个字段的内容。
1、第一方式是将所有分表数据进行union all
- select * from apporder.ord_shopping_order
- union all
- select * from apporder.ord_shopping_order_2
- union all
- select * from apporder.ord_shopping_order_3
- union all
- select * from apporder.ord_shopping_order_4
- union all
- select * from apporder.ord_shopping_order_5
- union all
- select * from apporder.ord_shopping_order_6
- union all
- select * from apporder.ord_shopping_order_7
- union all
- select * from apporder.ord_shopping_order_8
- union all
- select * from apporder.ord_shopping_order_9
- union all
- select * from apporder.ord_shopping_order_10
- union all
- select * from apporder.ord_shopping_order_11
- union all
- select * from apporder.ord_shopping_order_12
- union all
- select * from apporder.ord_shopping_order_13
- union all
- select * from apporder.ord_shopping_order_14
- union all
- select * from apporder.ord_shopping_order_15
- union all
- select * from apporder.ord_shopping_order_16
- union all
- select * from apporder.ord_shopping_order_17
- union all
- select * from apporder.ord_shopping_order_18
- union all
- select * from apporder.ord_shopping_order_19
- union all
- select * from apporder.ord_shopping_order_20
- union all
- select * from apporder.ord_shopping_order_21
- union all
- select * from apporder.ord_shopping_order_22
- union all
- select * from apporder.ord_shopping_order_23
- union all
- select * from apporder.ord_shopping_order_24
- union all
- select * from apporder.ord_shopping_order_25
- union all
- select * from apporder.ord_shopping_order_26
- union all
- select * from apporder.ord_shopping_order_27
- union all
- select * from apporder.ord_shopping_order_28
- union all
- select * from apporder.ord_shopping_order_29
- union all
- select * from apporder.ord_shopping_order_30
- union all
- select * from apporder.ord_shopping_order_31
- union all
- select * from apporder.ord_shopping_order_32
- union all
- select * from apporder.ord_shopping_order_33
- union all
- select * from apporder.ord_shopping_order_34
- union all
- select * from apporder.ord_shopping_order_35
- union all
- select * from apporder.ord_shopping_order_36
- union all
- select * from apporder.ord_shopping_order_37
- union all
- select * from apporder.ord_shopping_order_38
- union all
- select * from apporder.ord_shopping_order_39
- union all
- select * from apporder.ord_shopping_order_40
- union all
- select * from apporder.ord_shopping_order_41
- union all
- select * from apporder.ord_shopping_order_42
- union all
- select * from apporder.ord_shopping_order_43
- union all
- select * from apporder.ord_shopping_order_44
- union all
- select * from apporder.ord_shopping_order_45
- union all
- select * from apporder.ord_shopping_order_46
- union all
- select * from apporder.ord_shopping_order_47
- union all
- select * from apporder.ord_shopping_order_48
- union all
- select * from apporder.ord_shopping_order_49
- union all
- select * from apporder.ord_shopping_order_50
- union all
- select * from apporder.ord_shopping_order_51
- union all
- select * from apporder.ord_shopping_order_52
- union all
- select * from apporder.ord_shopping_order_53
- union all
- select * from apporder.ord_shopping_order_54
- union all
- select * from apporder.ord_shopping_order_55
- union all
- select * from apporder.ord_shopping_order_56
- union all
- select * from apporder.ord_shopping_order_57
- union all
- select * from apporder.ord_shopping_order_58
- union all
- select * from apporder.ord_shopping_order_59
- union all
- select * from apporder.ord_shopping_order_60
- union all
- select * from apporder.ord_shopping_order_61
- union all
- select * from apporder.ord_shopping_order_62
- union all
- select * from apporder.ord_shopping_order_63;

2、第二种方式是创建一个存储过程,方便以后更快速查询
先建一个表结构与 分表一样的表 ord_shopping_procedure
create table ord_shopping_procedure as select * from ord_shopping_order_2;
create table ord_shopping_procedure as select * from ord_shopping_order_2
然后创建存储过程
- CREATE DEFINER=`root`@`%` PROCEDURE `apporder`.`ord_shopping`()
- begin
- truncate table ord_shopping_procedure;
- insert into ord_shopping_procedure
- select * from apporder.ord_shopping_order
- union all
- select * from apporder.ord_shopping_order_2
- union all
- select * from apporder.ord_shopping_order_3
- union all
- select * from apporder.ord_shopping_order_4
- union all
- select * from apporder.ord_shopping_order_5
- union all
- select * from apporder.ord_shopping_order_6
- union all
- select * from apporder.ord_shopping_order_7
- union all
- select * from apporder.ord_shopping_order_8
- union all
- select * from apporder.ord_shopping_order_9
- union all
- select * from apporder.ord_shopping_order_10
- union all
- select * from apporder.ord_shopping_order_11
- union all
- select * from apporder.ord_shopping_order_12
- union all
- select * from apporder.ord_shopping_order_13
- union all
- select * from apporder.ord_shopping_order_14
- union all
- select * from apporder.ord_shopping_order_15
- union all
- select * from apporder.ord_shopping_order_16
- union all
- select * from apporder.ord_shopping_order_17
- union all
- select * from apporder.ord_shopping_order_18
- union all
- select * from apporder.ord_shopping_order_19
- union all
- select * from apporder.ord_shopping_order_20
- union all
- select * from apporder.ord_shopping_order_21
- union all
- select * from apporder.ord_shopping_order_22
- union all
- select * from apporder.ord_shopping_order_23
- union all
- select * from apporder.ord_shopping_order_24
- union all
- select * from apporder.ord_shopping_order_25
- union all
- select * from apporder.ord_shopping_order_26
- union all
- select * from apporder.ord_shopping_order_27
- union all
- select * from apporder.ord_shopping_order_28
- union all
- select * from apporder.ord_shopping_order_29
- union all
- select * from apporder.ord_shopping_order_30
- union all
- select * from apporder.ord_shopping_order_31
- union all
- select * from apporder.ord_shopping_order_32
- union all
- select * from apporder.ord_shopping_order_33
- union all
- select * from apporder.ord_shopping_order_34
- union all
- select * from apporder.ord_shopping_order_35
- union all
- select * from apporder.ord_shopping_order_36
- union all
- select * from apporder.ord_shopping_order_37
- union all
- select * from apporder.ord_shopping_order_38
- union all
- select * from apporder.ord_shopping_order_39
- union all
- select * from apporder.ord_shopping_order_40
- union all
- select * from apporder.ord_shopping_order_41
- union all
- select * from apporder.ord_shopping_order_42
- union all
- select * from apporder.ord_shopping_order_43
- union all
- select * from apporder.ord_shopping_order_44
- union all
- select * from apporder.ord_shopping_order_45
- union all
- select * from apporder.ord_shopping_order_46
- union all
- select * from apporder.ord_shopping_order_47
- union all
- select * from apporder.ord_shopping_order_48
- union all
- select * from apporder.ord_shopping_order_49
- union all
- select * from apporder.ord_shopping_order_50
- union all
- select * from apporder.ord_shopping_order_51
- union all
- select * from apporder.ord_shopping_order_52
- union all
- select * from apporder.ord_shopping_order_53
- union all
- select * from apporder.ord_shopping_order_54
- union all
- select * from apporder.ord_shopping_order_55
- union all
- select * from apporder.ord_shopping_order_56
- union all
- select * from apporder.ord_shopping_order_57
- union all
- select * from apporder.ord_shopping_order_58
- union all
- select * from apporder.ord_shopping_order_59
- union all
- select * from apporder.ord_shopping_order_60
- union all
- select * from apporder.ord_shopping_order_61
- union all
- select * from apporder.ord_shopping_order_62
- union all
- select * from apporder.ord_shopping_order_63;
-
- truncate table ord_shopping;
- insert into ord_shopping
- select t.*, JSON_EXTRACT(t.json, '$.skuName') as sku_name from (
- select id,order_type,order_mode,user_id,user_name,trade_no,state,total_amount, pay_amount pay_time ,
- ou_name ,mobile ,create_time, sale_amount, JSON_EXTRACT(item_abstract_json, '$[0]') as json,store_ou_code,store_name
- ,mch_ou_code,mch_name
- from ord_shopping_procedure ) t;
-
- END;

由于表中存在JSON字段,同时我们再查询表是应查询需要的字段即可
所以这一段是重点
truncate table ord_shopping;
insert into ord_shopping
select t.*, JSON_EXTRACT(t.json, '$.skuName') as sku_name from (
select id,order_type,order_mode,user_id,user_name,trade_no,state,total_amount, pay_amount pay_time ,
ou_name ,mobile ,create_time, sale_amount, JSON_EXTRACT(item_abstract_json, '$[0]') as json,store_ou_code,store_name
,mch_ou_code,mch_name
from ord_shopping_procedure ) t;
- truncate table ord_shopping;
- insert into ord_shopping
- select t.*, JSON_EXTRACT(t.json, '$.skuName') as sku_name from (
- select id,order_type,order_mode,user_id,user_name,trade_no,state,total_amount, pay_amount pay_time ,
- ou_name ,mobile ,create_time, sale_amount, JSON_EXTRACT(item_abstract_json, '$[0]') as json,store_ou_code,store_name
- ,mch_ou_code,mch_name
- from ord_shopping_procedure ) t;
JSON_EXTRACT(item_abstract_json, '$[0]') as json
表示item_abstract_json这字段存储的JSON字符串数组,我们把下标为0的取出来,
样例数据是这样的
- [{
- "id": 1059,
- "productType": 1,
- "productTypeSub": null,
- "skuName": "L’Oréal 欧莱雅男士锐能抗皱紧致洁面膏",
- "skuId": 194538,
- "skuCode": "200015112056194538",
- "thirdCode": "C023338",
- "isGift": 0,
- "scale": null,
- "unit": null,
- "brandId": 1707,
- "storeOuCode": "1043002001",
- "number": 1,
- "specs": "规格:100ML ",
- "cls1StaId": 1316,
- "cls2StaId": 16831,
- "cls3StaId": 16840,
- "cls1StaName": "美妆护肤",
- "cls2StaName": "男士护肤",
- "cls3StaName": "男士洁面",
- "phPictureUrl": "https:///default_file/00/01/52/02.jpg",
- "activityId": null,
- "activityTitle": null,
- "selfActivityId": null,
- "selfActivityTitle": null,
- "selfActivityType": null,
- "randomDeductionActivityId": null,
- "randomDeductionActivityTitle": null,
- "salePrice": 28.0,
- "realPrice": 28,
- "pointsPrice": 0,
- "rewardsPrice": 0,
- "empRewardsPrice": 0,
- "enterprisePrice": 0,
- "eleType": null,
- "eleDetailJson": null,
- "thirdServeId": null,
- "lineNo": "1",
- "packageItem": [],
- "barcode": null,
- "exchangeJumpAddress": null,
- "skuSaleScale": null,
- "perWeight": null,
- "externalNumber": null,
- "externalPrice": null,
- "deliverType": 2,
- "serveHour": null,
- "serveFlag": 0,
- "qyMerchant": null,
- "qyAlias": null,
- "distributionHigherReferrerId": null,
- "thirdpartyDeliveryTime": null,
- "thirdpartyCompleteTime": null
- }]

然后我们只需要取JSON的 skuName的内容就要用到这个函数
JSON_EXTRACT(t.json, '$.skuName') as sku_name
执行完存储过程,最后我们查询一下这个表ord_shopping
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。