将两张表数据汇总为一张表并展示

例如:取用户t_c_receive t_c_repayment 的交易金额合计并按金额大小倒序排列

select SUM(money) as num,userInfoId from 
(
select SUM(amount) as money,userInfoId  from t_c_receive 
where `status`=1 and createDate >='2019-08-15 00:00:00' and createDate<='2019-08-15 23:59:59' GROUP BY userInfoId 

union all
select SUM(repaymentRealSum) as money,userInfoId from t_c_repayment 
where `status`=1 and type=0 and repaymentTime >= '2019-08-15 00:00:00' and repaymentTime<='2019-08-15 23:59:59' GROUP BY userInfoId
) as t 
GROUP BY userInfoId order by SUM(money) desc limit 20

解析:t_c_receive t_c_repayment 取相同属性的字段,并把字段别名改为一致

union all将两表连接起来,组成新表,然后按正常操作,取想要的属性就ok了

----------------------------假如我是分界线,嘻嘻-------------------------------------------------------------------------------------------------------------

以此类推:例如:取用户t_c_receive t_c_repayment 的总交易次数
select SUM(count) num,userInfoId from 
(
select COUNT(1) as count,userInfoId  from t_c_receive 
where `status`=1 and userInfoId= 'TQRCgMfD07RNGsOjY0J'

union all
select COUNT(1) as count,userInfoId from t_c_repayment 
where `status`=1 and type=1 and userInfoId ='TQRCgMfD07RNGsOjY0J'
) as t 
GROUP BY userInfoId

操作都是一样的,难者不会,会者不难。。。。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值