首頁  >  問答  >  主體

mysql - sql 不用group by 怎么求和

对uid 相同的记录的money 求和

比如uid 387 的记录 结果
uid 金额 求和的金额
387 2000.00 5000.00
387 3000.00 5000.00

因为我需要记录的明细 所有不能用group by

怪我咯怪我咯2742 天前860

全部回覆(2)我來回復

  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:18:11

    我知道SQL語法很強大,不過我也不是很精通,針對你的需求,我提供以下參考意見。

    select a.uid,a.money,c.total_money from (select b.uid,sum(b.money) as total_money from leshi_deal_invest as b group by b.uid) as c 
    right join leshi_deal_invest as a 
    on a.uid = c.uid;

    回覆
    0
  • PHP中文网

    PHP中文网2017-04-17 13:18:11

    select b.uid, b.money, a.total from (select uid, sum(money) as total from test group by uid) as a, test as b where a.uid = b.uid
    

    夠懶的

    回覆
    0
  • 取消回覆