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

 一、前言

本次查询是在未知如何分库分表的情况下,对表数据进行查询,其中有的字段为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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/225062.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

记录本地与服务器之间数据传输方法(上传、下载文件)

文章目录 一、使用scp命令实现参数说明示例说明 二、使用工具实现windows系统苹果系统如有启发,可点赞收藏哟~ 一、使用scp命令实现 scp 是 secure copy (安全复制)的缩写, scp 是基于 ssh 登陆进行安全的远程文件拷贝命令。相当于 cp 命令 …

<Linux>(极简关键、省时省力)《Linux操作系统原理分析之Linux 进程管理 5》(9)

《Linux操作系统原理分析之Linux 进程管理 5》(9) 4 Linux 进程管理4.5 Linux 信号4.5.1 信号的作用和种类1.信号机制2.信号种类 4.5.2 信号的处理4.5.3 信号处理函数1.数据结构2. 处理函数 signal3.程序例 4 Linux 进…

Redis 两种持久化方式 AOF 和 RDB

目录 一、Redis 的持久化 二、Redis 的持久化方式 RDB RDB 介绍 RDB 的触发方式:. 三、RDB的文件生成策略 四、Save 和 Bgsave 命令的区别 六、RDB 最佳配置 七、触发机制-不容忽略方式 AOF 一、AOF介绍 二、RDB所存在的问题 三、AOF 三种策略 四、AOF…

SSM卫生信息管理系统开发mysql数据库web结构java编程计算机网页源码eclipse项目

一、源码特点 SSM 卫生信息管理系统是一套完善的信息系统,结合springMVC框架完成本系统,对理解JSP java编程开发语言有帮助系统采用SSM框架(MVC模式开发),系统具有完整的源代码和数据库,系统主要采用B/S模…

2023年“福建省工业互联网+智能制造创新大赛”开启报名

11月22日,由福建省总工会、福建省大数据集团有限公司共同举办的2023年“福建省工业互联网智能制造创新大赛”启动报名。 大赛积极响应《福建省总工会等八部门关于广泛深入开展劳动和技能竞赛为新发展阶段新福建建设建工立业的意见》(闽工〔2022〕70号&am…

算法基础之单链表

单链表 核心思想&#xff1a; 用数组模拟链表(new节点非常慢 用数组模拟快) e[N] 表示节点value ne[N]表示next指针指向 (空节点为-1) #include<iostream>using namespace std;const int N100010;//head头结点的指针//e[N] 表示节点value ne[N]表示next指针指向 //idx…

CentOS 系列:CentOS 7文件系统的组成

CentOS 7文件系统的组成 文件系统的组成Linux的一些重要目录文件和目录名主机名文件权限绝对路径和相对路径绝对路径相对路径 文件系统的组成 一切从根开始 文件路径中只有第一个/是根目录&#xff0c;后面的/是分隔符 文件名区分大小写 除斜线(/)以外&#xff0c;其他的字符…

FinOps和DevOps的未来会怎样?

FinOps&#xff08;或财务运营&#xff09;是一种文化实践&#xff0c;它将财务责任引入云的可变支出模型。这是一种将系统、最佳实践和文化相结合的战略方法&#xff0c;可提高组织了解云成本并做出明智决策的能力。 本质上&#xff0c;FinOps 是一个管理云运营费用&#xff…

有哪些值得推荐的数据可视化工具?

1 数据可视化工具的种类和应用场景 数据可视化工具的多样性使其能够满足不同用户的需求。一般而言&#xff0c;这些工具可分为开源版和商业版两大类。开源版特点&#xff1a;自由开源&#xff1a; 开源版数据可视化工具通常以免费形式提供&#xff0c;允许用户自由使用和修改源…

C/C++ Zlib实现文件压缩与解压

在软件开发和数据处理中&#xff0c;对数据进行高效的压缩和解压缩是一项重要的任务。这不仅有助于减小数据在网络传输和存储中的占用空间&#xff0c;还能提高系统的性能和响应速度。本文将介绍如何使用 zlib 库进行数据的压缩和解压缩&#xff0c;以及如何保存和读取压缩后的…

用C++和python混合编写数据采集程序?

之前看过一篇文章&#xff0c;主要阐述的就是多种语言混合编写爬虫程序&#xff0c;结合各种语言自身优势写一个爬虫代码是否行得通&#xff1f;觉得挺有意思的&#xff0c;带着这样的问题&#xff0c;我尝试着利用我毕生所学写了一段C和python混合爬虫程序&#xff0c;目前运行…

电脑如何录音?适合初学者的详细教程

“电脑怎么录音呀&#xff1f;参加了一个学校举办的短视频大赛&#xff0c;视频拍摄都很顺利&#xff0c;音乐却出了问题&#xff0c;朋友说可以用电脑录制一段音乐应付一下&#xff0c;可是我不会操作&#xff0c;有哪位大佬教教我&#xff01;” 声音是一种强大的媒介&#…