MySQL5.7和8.0版本,不同组内排序⽅法——定义变量@rank和
Row_number。。。
【5.7版本】:定义变量
1、按每个⼈的利润从⾼到低排序,并加上排名列
t@rank=0; #定义⼀个变量,初始值为1;下⾯没查询到⼀个结果,变量+1,并赋值给新的字段名rank_num
lect t1.*,@rank:=@rank+1as rank_num
from (
lect name_id,sum(profit)
from orders
group by name_id
order by sum(profit)DESC) as t1
2、组内排序:将每个产品在不同地区的产⽣利润排序
t@rank=0,@type="" #定义两个变量,⼀个规定排名逐渐+1,⼀定规定着product_id 如果改变,rank将重新赋值为1
lect t2.*,
@rank:=ca when@type= t2.product_id then@rank+1el1end as rank_num
@type:= t2.product_id as type
蜜蜂歌曲from (
lect product_id,area,sum(profit)
宵衣旰食from orders
group by product_id,area
order by product_id,sum(profit) DESC) as t2宣传物料
【8.0版本】:Row_number()over() 函数吃栗子
1、按每个⼈的利润从⾼到低排序,并加上排名列
建筑专业大学排名
lect name_id,sum(profit),Row_number()over(order by sum(profit) DESC) as'rank_num'
from orders
group by name_id
韩国的历史2、组内排序:将每个产品在不同地区的产⽣利润排序
lect product_id,area,sum(profit),Row_number()over(partition by product_id order by sum(profit) DESC) as'rank_num'
from orders
group by product_id,area小学生五年级日记
>q币查询