前情提要:

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

下面记录我的查询过程。

查询过程:

刚开始,第一步我以提现表为主表,查询出来相关结果。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 ;

查询结果如图是正常的:

接下来,我在左链接上订单表的数据,又添加一个了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 ;

查询结果对比图如下:

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

优化了思路: 把提现的统计好,把订单的统计好, 最后两个结果集再根据供应商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

正确的结果截图:

总结:

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

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