MySQL 两张表数据合并的实现
作者:Dylan 发布时间:2024-01-28 07:25:49
标签:MySQL,数据合并
有一个需求, 需要从数据库中导出两张表的数据到同一个excel中
鉴于是临时的业务需求, 直接使用Navicat 进行查询并导出数据.
数据涉及到三张表
CREATE TABLE `bigdata_qiye` (
`id` bigint(64) NOT NULL COMMENT '主键',
`tenant_id` varchar(12) DEFAULT '000000' COMMENT '租户ID',
`registration_type` int(2) DEFAULT NULL COMMENT '注册类型(1.国有,2.民营,3.外资)',
PRIMARY KEY (`id`) USING BTREE,
KEY `bigdata_qiye_tenant_id` (`tenant_id`) USING BTREE,
KEY `bigdata_qiye_id` (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='申报企业表';
CREATE TABLE `bigdata_qiye_report` (
`id` bigint(64) NOT NULL COMMENT '主键',
`tenant_id` varchar(12) DEFAULT '000000' COMMENT '租户ID',
`qiye_id` bigint(64) DEFAULT '0' COMMENT '企业扩展信息',
`revenue` double(16,2) DEFAULT NULL COMMENT '营收',
PRIMARY KEY (`id`) USING BTREE,
KEY `bqr_qiye_id` (`qiye_id`) USING BTREE,
KEY `bgr_tenant_id` (`tenant_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='企业申报信息表';
CREATE TABLE `bigdata_tech_improve_impl` (
`id` bigint(64) unsigned zerofill NOT NULL COMMENT '主键',
`tenant_id` varchar(12) DEFAULT '000000' COMMENT '租户ID',
`qiye_id` bigint(64) DEFAULT '0' COMMENT '企业扩展信息',
`total_input` decimal(64,2) DEFAULT NULL COMMENT '总投资',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='技改项目实施情况表';
需要合并导出 bigdata_qiye_report 表与 bigdata_tech_improve_impl 表的数据
表 bigdata_qiye 与表 bigdata_qiye_report 是 一对多的关系
表 bigdata_qiye 与表 bigdata_tech_improve_impl 也是 一对多的关系
表 bigdata_qiye_report 与表 bigdata_tech_improve_impl 没有关联关系
希望导出的excel格式
所以, 如果用链接查询的话产生的结果会偏差
比如这样
select bq.registration_type ,
bqr.revenue,
btii.total_input
from bigdata_qiye bq
left join bigdata_qiye_report bqr
on bqr.qiye_id = bq.id
left join bigdata_tech_improve_impl btii
on btii.qiye_id = bq.id
会产生许多的重复数据 .
解决方法
使用 union(对结果集去重) 或者 union all(不去重) 关键字 将两个 select 语句的结果作为一个整体显示出来
第一个sql
select
case
bq.registration_type
when 1 then '国有'
when 2 then '民营'
when 3 then '外资'
else ''
end as '注册类型',
bqr.revenue as '营收'
from bigdata_qiye bq
left join bigdata_qiye_report bqr
on bqr.qiye_id = bq.id
第二个sql
select
case
bq.registration_type
when 1 then '国有'
when 2 then '民营'
when 3 then '外资'
else ''
end as '注册类型',
btii.total_input as '总资产'
from bigdata_qiye bq
left join bigdata_tech_improve_impl btii
on btii.qiye_id = bq.id
合并 SQL
(select
case
bq.registration_type
when 1 then '国有'
when 2 then '民营'
when 3 then '外资'
else ''
end as '注册类型',
bqr.revenue as '营收'
from bigdata_qiye bq
left join bigdata_qiye_report bqr
on bqr.qiye_id = bq.id)
union all
(select
case
bq.registration_type
when 1 then '国有'
when 2 then '民营'
when 3 then '外资'
else ''
end as '注册类型'
btii.total_input as '总资产'
from bigdata_qiye bq
left join bigdata_tech_improve_impl btii
on btii.qiye_id = bq.id)
执行, 报错
原因: 使用 union 关键字时, 必须要保证两张表的字段一模一样(包括顺序)
所以 修改sql
sql _1 修改
select
case
bq.registration_type
when 1 then '国有'
when 2 then '民营'
when 3 then '外资'
else ''
end as '注册类型',
bqr.revenue as '营收',
'' as '总资产'
from bigdata_qiye bq
left join bigdata_qiye_report bqr
on bqr.qiye_id = bq.id
sql_2修改
select
case
bq.registration_type
when 1 then '国有'
when 2 then '民营'
when 3 then '外资'
else ''
end as '注册类型',
'' as '营收',
btii.total_input as '总资产'
from bigdata_qiye bq
left join bigdata_tech_improve_impl btii
on btii.qiye_id = bq.id
合并SQL
(select
case
bq.registration_type
when 1 then '国有'
when 2 then '民营'
when 3 then '外资'
else ''
end as '注册类型',
bqr.revenue as '营收',
'' as '总资产'
from bigdata_qiye bq
left join bigdata_qiye_report bqr
on bqr.qiye_id = bq.id)
union all
(select
case
bq.registration_type
when 1 then '国有'
when 2 then '民营'
when 3 then '外资'
else ''
end as '注册类型',
'' as '营收',
btii.total_input as '总资产'
from bigdata_qiye bq
left join bigdata_tech_improve_impl btii
on btii.qiye_id = bq.id)
查询结果
来源:https://blog.csdn.net/weixin_44931584/article/details/127211386


猜你喜欢
- #!/usr/bin/env pythonimport sys,timefrom socket import socketdef read_
- 1,执行SQL查看select @@session.sql_mode;全局级别: 查看select @@global.sql_mode;2,
- python如何更新修改后的Python模块1.利用python的MySQLdb模块利用原生的sql语句进行更新的方法代码配置方法代码2.使
- 前言前段时间在微博看到一段摸鱼人的倒计时模板,感觉还挺有趣的。于是我用了一小时的时间写了个页面出来 摸鱼办地址 (当然是摸鱼的时间啦)。模板
- # 贪婪模式 默认的匹配规则# 在满足条件的情况下 尽可能多的去匹配到字符串import rers = re.match('\d{6
- 有时候我们会截取字符串中的一些特殊想要的东西,大小写字母、模号、汉字、数字等等,今天先介绍一个获取字符串中小写字母(也是大写字母)的方式,直
- 利用numpy和scipy,我们可以很容易根据欧拉角求出旋转矩阵,这里的旋转轴我们你理解成四元数里面的旋转轴 import nu
- 项目演示:一、输入金额二、跳转到支付宝付款三、支付成功四、跳转回自己网站在使用支付宝接口的前期准备:1、支付宝公钥2、应用公钥3、应用私钥4
- 本文实例讲述了js控制多图左右滚动切换效果。分享给大家供大家参考。具体如下:这是一款纯js实现点击左右按钮图片自动左右平滑滚动,默认5个一组
- 本文实例讲述了Python内存管理。分享给大家供大家参考,具体如下:a = 1a是引用,1是对象。Python缓存整数和短字符串,对象只有一
- 问题你想自己去实现一个新的上下文管理器,以便使用with语句。解决方案实现一个新的上下文管理器的最简单的方法就是使用 contexlib 模
- 遇到这么个需求:把图片按照定义的patchsize切块,然后按照z轴顺序叠放小块,如下图(仅考虑灰度图像)图片im,设size为(h,w),
- 本文实例为大家分享了python使用matplotlib绘制雷达图的具体代码,供大家参考,具体内容如下示例代码:# encoding: ut
- 今日在Stack Overflow上看到一个问如何只初始化未初始化的变量,有人提供了一个函数,特地粘贴过来共大家品鉴:import tens
- if exists (select * from dbo.sysobjects where id = object_id(N'[db
- 在Web开发者中,Google Chrome是使用最广泛的浏览器。六周一次的发布周期和一套强大的不断扩大开发功能,使其成为了web开发者必备
- 虽然ting88没有注册的用户不能下载歌曲,但搞定它也非难事啊:)进入www.ting88.com的网站,把歌手专辑页面的URL复制到文本框
- 方法一、简单安装(通过yum)1.安装epel-releaserpm -ivh http://dl.fedoraproject.
- 独立 fmt Log输出重定向golang的fmt包的输出函数 Println、Printf、PrintStack等,默认将打印输出到os.
- 导读:这篇论坛文章主要介绍了使用SQL Server升级顾问的具体步骤,详细内容请参考下文。微软提供了SQL Server 2008升级顾问