1

我的表如下所示:

shiftno     sno     Amount
  100         7       20
              8       50
  101         9       10
             10       30
             11       20

我试过这个查询

select SUM(Amount) 
from example 
where shiftNo = (select (max(shiftNo)) from example

它显示结果 = 10,但实际结果是 10 + 30 + 20 = 60。属于 shiftno=101

我怎样才能得到60的总结果?

4

1 回答 1

4

如果你想要最高的shiftNo使用limit 1
如果你想要所有shiftNo和他们amount然后离开这limit条线:

 select shiftNo, SUM(Amount) as MaxAmount
 from exmple 
 group by shiftNo
 order by shiftNo desc
 limit 1
于 2012-06-08T13:02:31.040 回答