Mysql根据其他列中的值在列中添加值

时间:2012-09-25 10:17:53

标签: mysql

Payment Table

从表中获取以下结果的mysql查询是什么?应根据invoiceID执行金额列中的添加。

enter image description here

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

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