mysql 生成排名字段
mysql 生成 排名
2023-09-14 09:08:27 时间
假设有test表,下图为表机构和数据,score表示积分。现在要查询积分排名为第几的id??
查询语句
- select id,score,(@rowno:=@rowno+1) as rowno from test,(select (@rowno:=0)) b order by score desc;
查询结果:
这样就得到了根据积分排名的位置了(rowno),要获得id只要在这个表的基础上进行查询就可以了
- select id from (select id,score,(@rowno:=@rowno+1) as rowno from test,(select (@rowno:=0)) b order by score desc) c where rowno=1;
rowno的值为你想要查询的名次,就得到对应的id了。
上面的rowno为1,排名第一的id就是2了
一行算这次拍名和上次排名的差:
select aa.id,aa.rowno, bb.rowno, aa.rowno-bb.rowno from
(SELECT * , (@rowno:=@rowno+1) as rowno FROM statisticsystem.report_live_hits a ,(select (@rowno:=0)) b order by duration) as aa,
(SELECT * , (@rowno1:=@rowno1+1) as rowno FROM statisticsystem.report_live_hits a ,(select (@rowno1:=0)) b order by users) as bb
where aa.main_id = bb.main_id and aa.cate_id=bb.cate_id and aa.sub_id=bb.sub_id and aa.root_id=bb.root_id ;
相关文章
- MySQL 生成报表技术探究(mysql生成报表)
- MySQL序号自动生成简单方法(mysql显示序号)
- MySQL快速生成数据库的方法(mysql生成数据库)
- MySQL自动生成主键的好处(mysql生成主键)
- 谁是MySQL中分数排名前列的?(分数排名mysql)
- MySQL查询优化:提升匹配度(mysql匹配度)
- MySQL中如何生成指定范围的随机数(mysql随机数范围)
- mysql对比:Redis vs. MySQL(redisx)
- MySQL分布式技术:分布式主键的生成与应用(mysql分布式主键)
- MySQL数据库文档的自动生成指南(mysql数据库文档生成)
- MySQL:历史上技术强国排名第几?(mysql排名第几)
- 如何在 MySQL 中生成随机数据?——学习 MySQL 随机数据生成技巧(mysql随机数据)
- MySQL使用语句查询数据(mysql中使用语句吗)
- C语言搭建MySQL服务器 - 技术指引(c mysql 服务器)
- MySQL使用Bitmap索引提高查询效率(bitmap mysql)
- MySQL创建表示例快速掌握MYSQL基础操作(mysql中创建表的例子)
- MySQL的YEAROF函数用于获取日期或日期时间表达式中的年份(mysql yearof)
- MySQL查询如何查询两字段相等(mysql两字段相等查询)