mysql自动生成排序列复杂sql

本文介绍了一种使用SQL语句实现自定义排名的方法。通过设置变量@rownum进行累加实现行号功能,并结合具体业务场景展示了如何对股票竞赛玩家按不同利润比率进行排序。该方法适用于需要对查询结果进行快速排名的场合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用sql语句原功能

SELECT @rownum := @rownum+1 AS rownum, 

               table1.* 

  FROM (SELECT @rownum:=0) r 

               table1; 


生成语句

SELECT @rownum:=@rownum+1 AS rank,t.*  FROM (SELECT @rownum:=0+"+num+") r,
(select distinc p.name,p.type,p.salaryno,h.day_profit_rate as profit,h.month_profit_rate,h.quarter_profit_rate,h.total_profit_rate,
h.competition_total_asset,h.account_total_asset,h.competition_total_asset/h.account_total_asset as ratio,
h.competition_month_average_asset,h.competition_quarter_average_asset,h.competition_total_average_asset,h.sj
from stock_competition_player p join stock_competition_user_info_history h 
on p.salaryno = h.salaryno  order by h.sj desc, h.day_profit_rate desc )t

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值