简体   繁体   中英

Mysql Adding values in a column based on values in other column

付款表

What is the mysql query to get the below result from the table?. Addition in the amount column should be carried out based on the invoiceID.

在此处输入图片说明

SELECT @rownum := @rownum + 1 AS ID, InvoiceID, SUM(AMOUNT) 
FROM <tablename>, (SELECT @rownum := 0) r
GROUP BY InvoiceID

try this:

select  @i:=@i+1 AS id,a.*
     from
     (select InvoiceID,sum(Amount) as Amount
      from your_table
      group by InvoiceID)a,(SELECT @i:=0) r 


SQL Fiddel demo

select ID,InvoiceID, SUM(Amount)
from <table>
group by InvoiceID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM