Mysql 查询某条记录的行号(排序后)

假设有个表,有以下数据

在这里插入图片描述

假设:要求按着f_total_power充电量排序,充电量相同的情况下按着创建时间降序排(排名不存在并列)
1.先按着要求排序

先按着要求排好序
select tt.f_id,tt.f_team_name,tt.f_total_power,tt.f_create_time from t_fleet_team tt where tt.f_activity_id=99 ORDER BY tt.f_total_power desc,tt.f_create_time asc

在这里插入图片描述

2.排序后要获取每行行号

利用中间变量@rowno计算行号
select temp.f_id,temp.f_team_name,temp.f_total_power,temp.f_create_time,(@rowno:=@rowno+1) as no from (
select tt.f_id,tt.f_team_name,tt.f_total_power,tt.f_create_time from t_fleet_team tt where tt.f_activity_id=99 ORDER BY tt.f_total_power desc,tt.f_create_time asc
)temp,(select (@rowno:=0)) tnum

在这里插入图片描述

3.查询某一个ID的行号

利用子查询 查询某条数据的行号
select t.no from (
select temp.f_id,temp.f_team_name,temp.f_total_power,temp.f_create_time,(@rowno:=@rowno+1) as no from (
select tt.f_id,tt.f_team_name,tt.f_total_power,tt.f_create_time from t_fleet_team tt where tt.f_activity_id=99 ORDER BY tt.f_total_power desc,tt.f_create_time asc
)temp,(select (@rowno:=0)) tnum
)t where t.f_id=3;

假设:要求按着f_total_power充电量排序,并考虑并列排名--------

select count(DISTINCT(tt.f_total_power))+1 as no from t_fleet_team tt where tt.f_total_power > (select temp.f_total_power from t_fleet_team temp where temp.f_id=3)

oracle参考链接:

https://blog.csdn.net/shaiguchun9503/article/details/82349050

https://blog.csdn.net/qq_25221835/article/details/82762416?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值