例如:取用户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将两表连接起来,组成新表t ,然后按正常操作,取想要的属性就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
操作都是一样的,难者不会,会者不难。。。。。。。。。。