查看原文
其他

mySQL 中分组函数的写法

liveonsky twt企业IT社区 2022-07-03

--max最大值所有行
select it.user_id, 
it.income,
case
when @maxVal is not null then @maxVal := @maxVal
when @maxVal is null then @maxVal := it.income
end AS sum_income
from income_tl it, (select @maxVal:=null) r
order by it.income

select it.user_id, 
it.income,
@maxVal := IFNULL(@maxVal,it.income)
from income_tl it, (select @maxVal:=null) r
order by it.income

--max最大值,分组行
select it.user_id, 
it.income,
case
when @preVal = it.user_id then @maxVal := @maxVal
when @preVal := it.user_id then @maxVal := it.income
end AS sum_income
from income_tl it, (select @preVal:=null, @maxVal:=null) r
order by it.user_id asc, it.income asc;


长按二维码关注公众号

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存