Mysql 对一列求和后如何找到最大值和最小值

Mysql How can i find max and min after sum a column

This is my code from tables

我想 select 从它的前两行和最后两行作为结果。这可能吗?

换句话说,我想 select 列求和后的最小值和最大值。如果可能,最好各选两个 非常感谢

`select A.prod_id, SUM(quantity) Total 
 from charging A 
  group by prod_id 
  order by total`

您可以使用子查询。因为您懒得在问题中包含您的查询,所以我不会重新输入它。仅对于值:

select min(Total), max(Total)
from (<your query here>) s

对于具有两个值的四行:

(select t.*
 from (<your query here>) t
 order by Total asc
 limit 2
) union all
(select t.*
 from (<your query here>) t
 order by Total desc
 limit 2
)