记录一次如何查询mysql分库分表数据

一、前言

本次查询是在未知如何分库分表的情况下,对表数据进行查询,其中有的字段为JSON结构。需要提取JSON中某个字段的内容。

二、查询步骤

1、第一方式是将所有分表数据进行union all

sql 复制代码
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;

sql 复制代码
create table ord_shopping_procedure as  select  * from  ord_shopping_order_2

然后创建存储过程

sql 复制代码
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;

sql 复制代码
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的取出来,

样例数据是这样的

bash 复制代码
[{
	"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

相关推荐
weixin_5318042437 分钟前
SQL优化技巧(如查询优化、索引优化)。分布式系统的基本概念及挑战(如数据一致性、服务发现、负载均衡)
数据库·sql·服务发现
一 乐1 小时前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·数据库·学习·考研·微信·小程序·源码
一 乐1 小时前
租拼车平台|小区租拼车管理|基于java的小区租拼车管理信息系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·微信·notepad++·拼车
BillDev1 小时前
Oracle创建用户报错-ORA-65096: invalid common user or role name
数据库·oracle
szial2 小时前
深入理解SQL模糊查询中的特殊字符处理——以DB2为例
数据库·sql
jxxchallenger2 小时前
踩坑spring cloud gateway /actuator/gateway/refresh不生效
java·数据库·gateway
肥or胖2 小时前
【MySQL】索引
linux·数据库·mysql
Kiwi233332 小时前
产品经理-需求分析
数据库·产品经理·需求分析
读心悦3 小时前
修改 MySQL 数据库中的唯一键
数据库·mysql
卓琢3 小时前
(九)Shell 脚本(四):正则表达式、sed 和 awk 详解
linux·mysql·正则表达式