鍍金池/ 問答/PHP  數(shù)據(jù)庫/ php mysql分組后取分組中的第一條數(shù)據(jù)和最后一條數(shù)據(jù) 并且做統(tǒng)計這樣的MY

php mysql分組后取分組中的第一條數(shù)據(jù)和最后一條數(shù)據(jù) 并且做統(tǒng)計這樣的MYSQL怎么寫呢

圖片描述

需要按時間分組統(tǒng)計分組后的price總和 number總合 并要取出分組中的第一個price和最后一個price的值
找了好久找不到相應的方法希望各位能指導一下

回答
編輯回答
墨小羽

時間分組,排序字段不清,姑且按price排序吧

select 
    mtime,sum(price),sum(number),
    substring_index(group_concat(price order by price),',',1) min_price,
    substring_index(group_concat(price order by price),',',-1) max_price
from table group by mtime;
2018年8月5日 11:54
編輯回答
陪她鬧
select min(price) as min_price, max(price) as max_price from (select mtime, code, sum(price) as price, sum(number) as number from table_xx group by mtime order by mtime desc) data
2017年6月28日 17:57