liveonsky
作者liveonsky·2018-10-10 14:33
数据库开发工程师·sr

mysql中分组函数的写法

字数 926阅读 1541评论 0赞 3

--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;

--按顺序求最大值
select it.id,it. income
,case
when @maxVal >= it. income then @maxVal
else @maxVal := it. income
end AS max_ income
from income_tl it, (select @maxVal := null) r
order by it.id asc, it. income desc

参考:
https://blog.csdn.net/tangtong1/article/details/50996669/

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

3

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广