Mysql如何在总和列后找到最大值和最小值

时间:2015-06-14 13:23:32

标签: mysql sql mysql-workbench

This is my code from tables

我想从中选择前两行和后两行作为结果。那可能吗?

换句话说,我想在总和之后选择列的最小值和最大值。如果可能的话,更喜欢两个 非常感谢

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

1 个答案:

答案 0 :(得分:0)

您可以使用子查询。因为您不想在查询中包含您的查询,所以我不会重新输入它。仅供参考:

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
)