是这样的,从(账户表)account表里读取是代理代理商的账户,再在商家(shop)表里查询商家是属于哪个代理商(外键是daili_id就是代理商的编号),再在资金(money)里面查询每个商家的的总额(外键为shop_id),由于每条记录都有保留,根据shop_id在money中区最大的一条,再把属于一个代理商的的钱相加起来,欢迎大神指点
account(daili_id) shop(shop_id,daili_id) money(id,shop_id,moneys)
select a.daili_id,s.shop_id,max(m.moneys) from account a inner join shop s on a.daili_id=s.daili_id
inner join money m on m.shop_id=s.shop_id
group by a.daili_id,s.shop_id,参考一下,是否与你的需求相近,还需要完善。
uid就是daili_id一样的money要相加,其他的都好的
很相近了
@phpstart: 把上面的查询结果作为一个表在外面查询,根据daili_id分组,通过聚合函数sum(金额)实现money的累加。
@?,????: 求指导
SELECT t.uid, SUM(t.mx) AS allMoney FROM ( SELECT a.uid, s.shop_id, MAX(m.moneys) AS mx FROM account a INNER JOIN shop s ON a.uid = s.daili_id INNER JOIN MONEY m ON m.shop_id = s.shop_id GROUP BY a.uid, s.shop_id ) AS t GROUP BY t.uid ORDER BY t.uid
@?,????: 很对,就是这个效果
@phpstart:
这里不是根据max(moneys)来查找,而是根据money_id来查找,求指教
@?,????: 就是根据max(money_id)来查询,
去aid相同的id最大的一条
@phpstart:
SELECT t.uid, SUM(m2.money) AS allMoney--这里累加金额 FROM ( SELECT a.uid, s.shop_id, MAX(m.money_id) AS maxId--那么就改一下 FROM account a INNER JOIN shop s ON a.uid = s.daili_id INNER JOIN MONEY m ON m.shop_id = s.shop_id GROUP BY a.uid, s.shop_id ) AS t INNER JOIN MONEY m2 ON m2.money_id=t.maxId--连接最大ID GROUP BY t.uid ORDER BY t.uid
@?,????: 谢谢,完美解决问题