MySQL派生表联表查询实战过程


Posted in MySQL onMarch 20, 2022

前情提要:

公司运营的一个商城系统,忽然发现订单提现功能有问题,有大量的商户体现金额和订单金额不一致。于是产生了需求,需要把提现表和供应商表作为一个结果集,连接上订单表中的订单金额,通过计算订单表的金额和体现表商户提现的金额进行比对,查看商户是多提现了还是少提现了。

下面记录我的查询过程。

查询过程:

刚开始,第一步我以提现表为主表,查询出来相关结果。MySQL语句如下

SELECT  count(ysw.supply_id) AS '提现次数',ysw.user_id AS '供应商对应的用户ID', ysw.supply_id  AS '供应商ID' ,SUM(ysw.money)  AS '供应商提现总金额',
case ysw.pay_type when 10 then '微信' when 20 then '支付宝' else '银行卡' end as '支付方式' ,
ys.supply_name AS '供应商名称',ys.money AS '供应商余额',ys.freez_money AS '供应商冻结金额(已提现金额)'
FROM yoshop_supply_withdraw AS ysw LEFT JOIN yoshop_supply AS ys ON ysw.supply_id = ys.supply_id
WHERE ysw.create_time < 1647446400 AND ysw.apply_status IN (10,20,40) GROUP BY ysw.supply_id
ORDER BY SUM(ysw.money) DESC ;

查询结果如图是正常的:

MySQL派生表联表查询实战过程

接下来,我在左链接上订单表的数据,又添加一个了left join,金额相关数据发生了变化严重不一致,而且查询时间明显延长,MySQL语句如下

SELECT  count(ysw.supply_id) AS '提现次数',ysw.user_id AS '供应商对应的用户ID', ysw.supply_id  AS '供应商ID' ,SUM(ysw.money)  AS '供应商提现总金额',
case ysw.pay_type when 10 then '微信' when 20 then '支付宝' else '银行卡' end as '支付方式' ,
ys.supply_name AS '供应商名称',ys.money AS '供应商余额',ys.freez_money AS '供应商冻结金额(已提现金额)',SUM(yo.pay_price)

FROM yoshop_supply_withdraw AS ysw LEFT JOIN yoshop_supply AS ys ON ysw.supply_id = ys.supply_id
LEFT JOIN yoshop_order AS yo ON yo.supply_ids =ysw.supply_id 

WHERE ysw.create_time < 1647446400 AND ysw.apply_status IN (10,20,40) GROUP BY ysw.supply_id
ORDER BY SUM(ysw.money) DESC ;

查询结果对比图如下:

MySQL派生表联表查询实战过程

经过实践,我想直接通过左连接查询到提现表金额和订单表金额是行不通的。通过网上查资料,以及在技术群里请教,

优化了思路: 把提现的统计好,把订单的统计好, 最后两个结果集再根据供应商id做个链接

接下来就是,三步走了, 第一步:把提现的统计好,上面第一次尝试的第一步就是了, 第二步:把订单表的数据统计好。由于使用系统的原因,我直接使用的订单商品表计算的订单总金额,这一步也是分三步走的,我直接上代码:

1.查询yoshop_order所有进行中,已完成的 订单id(order_id);
	SELECT order_id FROM yoshop_order WHERE order_status IN (10,30);
	
2.查询没有退款的订单ID
	SELECT order_id FROM yoshop_order WHERE order_status IN (10,30) AND order_id NOT IN ( SELECT order_id FROM yoshop_order_refund);
	
3.查询订单商品表中 所有的订单金额

SELECT  supply_id  AS '供应商ID' , SUM(total_pay_price)  AS '供应商订单总金额' FROM yoshop_order_goods WHERE  create_time < 1647446400 AND order_pay_status = 0  AND  order_id IN(SELECT order_id FROM yoshop_order WHERE order_status IN (10,30) AND order_id NOT IN ( SELECT order_id FROM yoshop_order_refund)	 )  GROUP BY supply_id 
ORDER BY SUM(total_pay_price) DESC ;

接下来就是进行把第一步和第二步的查询结果当作派生表,进行左连接查询。我在这一步耗费的时间和精力最多。如果你能认真看完,相信一定会有收货。我在这里把我错误的过程也进行了记录 第一次错误拼接:

SELECT * FROM  (
	SELECT  count(ysw.supply_id) AS '提现次数',ysw.user_id AS '供应商对应的用户ID', ysw.supply_id  AS 'supply_id' ,SUM(ysw.money)  AS '供应商提现总金额',
	case ysw.pay_type when 10 then '微信' when 20 then '支付宝' else '银行卡' end as '支付方式' ,
	ys.supply_name AS '供应商名称',ys.money AS '供应商余额',ys.freez_money AS '供应商冻结金额(已提现金额)'
	FROM yoshop_supply_withdraw AS ysw LEFT JOIN yoshop_supply AS ys ON ysw.supply_id = ys.supply_id
	WHERE ysw.create_time < 1647446400 AND ysw.apply_status IN (10,20,40) GROUP BY ysw.supply_id
	ORDER BY SUM(ysw.money) DESC ) AS t1 
union all    // left join ,这里是注释记得删除

SELECT * FROM   --  这里是错误的不应该在查询
		(SELECT  supply_id  AS 'supply_id' , SUM(total_pay_price)  AS total_pay_price FROM yoshop_order_goods WHERE  create_time < 1647446400 AND order_pay_status = 0  AND  order_id IN(
	SELECT order_id FROM yoshop_order WHERE order_status IN (10,30) AND order_id NOT IN (
	SELECT order_id FROM yoshop_order_refund)	 )  GROUP BY supply_id 
ORDER BY SUM(total_pay_price) DESC ) AS t2
								
ON t1.suppply_id = t2.suppply_id

通过这一次试错,明显看出我把left join 和 union all 的含义记错了,并且在拼接的时候重复使用了select * from 。虽然是试错了,但也是有收货的,接下来进行了第二次错误的拼接:

SELECT t1.提现次数 ,t1.供应商对应的用户ID ,t1.supply_id, t1.支付方式 ,t1.供应商名称,t1.供应商余额, t1.供应商冻结金额(已提现金额), t2.total_pay_price FROM  (
SELECT  count(ysw.supply_id) AS '提现次数',ysw.user_id AS '供应商对应的用户ID', ysw.supply_id  AS supply_id ,SUM(ysw.money)  AS '供应商提现总金额',
case ysw.pay_type when 10 then '微信' when 20 then '支付宝' else '银行卡' end as '支付方式' ,
	ys.supply_name AS '供应商名称',ys.money AS '供应商余额',ys.freez_money AS '供应商冻结金额(已提现金额)'
	FROM yoshop_supply_withdraw AS ysw LEFT JOIN yoshop_supply AS ys ON ysw.supply_id = ys.supply_id
	WHERE ysw.create_time < 1647446400 AND ysw.apply_status IN (10,20,40) GROUP BY ysw.supply_id
	ORDER BY SUM(ysw.money) DESC ) AS t1 
        
 LEFT JOIN
		
(SELECT  supply_id  AS supply_id , SUM(total_pay_price)  AS total_pay_price FROM 
yoshop_order_goods WHERE  create_time < 1647446400 AND order_pay_status = 0  
AND  order_id IN(
SELECT order_id FROM yoshop_order WHERE order_status IN (10,30) 
AND order_id NOT IN (
SELECT order_id FROM yoshop_order_refund)	 )  
GROUP BY supply_id 
ORDER BY SUM(total_pay_price) DESC ) AS t2						
ON t1.suppply_id = t2.suppply_id

通过这两次错误的尝试,以及根据尝试过程中MySQL给出的错误提示,知道自己是在左连接上使用错误了,应该在开始查询出来所有的字段,left join 后不能在使用select * 最后,回想了一遍自己所学的left join的语法,写出了最后的正确的查询结果

SELECT t1.supply_id '供应商ID',t1.supply_name '供应商名称',t1.user_id '供应商绑定的用户ID',t1.withdrawtime '供应商提现次数' ,t1.supplyallmoney '供应商提现金额',t1.payway '供应商提现方式',t1.supply_money '供应商账户余额',t1.supply_free_money '供应商冻结余额(已提现金额)',
t2.total_pay_price '供应商订单总金额',t2.order_id '供应商订单数量'
FROM  (											
SELECT  count(ysw.supply_id) AS withdrawtime,  ysw.user_id AS user_id,   ysw.supply_id  AS supply_id ,  SUM(ysw.money)  AS supplyallmoney,   ysw.alipay_name AS alipay_name ,ysw.alipay_account AS alipay_account,  ysw.audit_time as audit_time ,  ysw.bank_account AS bank_account,   ysw.bank_card AS bank_card,   ysw.bank_name AS bank_name,
case ysw.pay_type when 10 then '微信' when 20 then '支付宝' else '银行卡' end as payway ,
ys.supply_name AS supply_name,  ys.money AS supply_money,  ys.freez_money AS supply_free_money
FROM yoshop_supply_withdraw AS ysw LEFT JOIN yoshop_supply AS ys ON ysw.supply_id = ys.supply_id
WHERE ysw.create_time < 1647446400 AND ysw.apply_status IN (10,20,40) GROUP BY ysw.supply_id
ORDER BY SUM(ysw.money) DESC ) AS t1 
	
 LEFT JOIN

    (SELECT  supply_id  AS 'supply_id' , COUNT(order_id) AS order_id,   SUM(total_pay_price)    AS total_pay_price 
    FROM 	yoshop_order_goods WHERE  create_time < 1647446400 AND order_pay_status = 0  
    AND  order_id IN(
        SELECT order_id FROM yoshop_order WHERE order_status IN (10,30) 
    AND order_id NOT IN (
        SELECT order_id FROM yoshop_order_refund)	 ) 
    GROUP BY supply_id 
    ORDER BY SUM(total_pay_price) DESC ) AS t2
								
ON t1.supply_id = t2.supply_id

正确的结果截图:

MySQL派生表联表查询实战过程

总结:

这次查询的经历使我自己明显的感觉到动手实战才能提升自己的能力,加强自己的记忆,在查询的时候要注意以下几点。第一:一步步的进行查询,不要害怕查询数据的复杂。第二:在派生表中尽量不要使用汉字作为字段名,只在最顶层select 查询最终结果的时候在把字段名 as 为“汉字”,第三:熟悉记忆左连接和union 链接的查询语法。

到此这篇关于MySQL派生表联表查询的文章就介绍到这了,更多相关MySQL派生表联表查询内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

MySQL 相关文章推荐
MySQL InnoDB ReplicaSet(副本集)简单介绍
Apr 24 MySQL
详解MySQL 联合查询优化机制
May 10 MySQL
MySQL中你可能忽略的COLLATION实例详解
May 12 MySQL
MySQL 查询速度慢的原因
May 25 MySQL
SQL注入的实现以及防范示例详解
Jun 02 MySQL
MySQL系列之三 基础篇
Jul 02 MySQL
QT连接MYSQL数据库的详细步骤
Jul 07 MySQL
基于MySql验证的vsftpd虚拟用户
Nov 07 MySQL
详解MySql中InnoDB存储引擎中的各种锁
Feb 12 MySQL
解决MySQL添加新用户-ERROR 1045 (28000)的问题
Mar 03 MySQL
MySQL如何快速创建800w条测试数据表
Mar 17 MySQL
MySQL导致索引失效的几种情况
Jun 25 MySQL
MySQL学习之基础命令实操总结
Mar 19 #MySQL
MySQL学习之基础操作总结
Mar 19 #MySQL
Pycharm远程调试和MySQL数据库授权问题
Mar 18 #MySQL
MySQL 主从复制数据不一致的解决方法
浅谈redis的过期时间设置和过期删除机制
MySQL读取JSON转换的方式
Mar 18 #MySQL
分享MySQL常用 内核 Debug 几种常见方法
Mar 17 #MySQL
You might like
php写入、删除与复制文件的方法
2015/06/20 PHP
PHPStrom中实用的功能和快捷键大全
2015/09/23 PHP
非常经典的PHP文件上传类分享
2016/05/15 PHP
php获取文章内容第一张图片的方法示例
2017/07/03 PHP
jquery获取input的value问题说明
2010/08/19 Javascript
JS和jquery获取各种屏幕的宽度和高度的代码
2013/08/02 Javascript
js给onclick赋值传参数的两种方法
2013/11/25 Javascript
js计算德州扑克牌面值的方法
2015/03/04 Javascript
JS验证邮件地址格式方法小结
2015/12/01 Javascript
javascript移动开发中touch触摸事件详解
2016/03/18 Javascript
Bootstrap 3.x打印预览背景色与文字显示异常的解决
2016/11/06 Javascript
JS弹性运动实现方法分析
2016/12/15 Javascript
JS变量中有var定义和无var定义的区别以及es6中let命令和const命令
2017/02/19 Javascript
js+html5实现半透明遮罩层弹框效果
2020/08/24 Javascript
JavaScript输入分钟、秒倒计时技巧总结(附代码)
2017/08/17 Javascript
Vue.js表单标签中的单选按钮、复选按钮和下拉列表的取值问题
2017/11/22 Javascript
Three.JS实现三维场景
2018/12/30 Javascript
[01:09:16]DOTA2-DPC中国联赛 正赛 SAG vs Dynasty BO3 第一场 1月25日
2021/03/11 DOTA
python获取beautifulphoto随机某图片代码实例
2013/12/18 Python
Python第三方库的安装方法总结
2016/06/06 Python
Python基于Pymssql模块实现连接SQL Server数据库的方法详解
2017/07/20 Python
利用numpy和pandas处理csv文件中的时间方法
2018/04/19 Python
python生成九宫格图片
2018/11/19 Python
Pycharm使用远程linux服务器conda/python环境在本地运行的方法(图解))
2019/12/09 Python
Tensorflow设置显存自适应,显存比例的操作
2020/02/03 Python
python计算波峰波谷值的方法(极值点)
2020/02/18 Python
Python局部变量与全局变量区别原理解析
2020/07/14 Python
Python中Pyspider爬虫框架的基本使用详解
2021/01/27 Python
汇科协同Java笔试题
2012/03/31 面试题
95%的面试官都会问到的50道Java线程题,附答案
2012/08/03 面试题
学生干部学习的自我评价
2014/02/18 职场文书
大学运动会入场词
2014/02/22 职场文书
公司开业庆典主持词
2014/03/21 职场文书
土地转让协议书
2014/04/15 职场文书
2014乡镇党政班子四风问题思想汇报
2014/09/14 职场文书
2014年行政后勤工作总结
2014/12/06 职场文书