MYSQL 排名方式汇总
来源:互联网 发布:淘宝店铺设置新品上架 编辑:程序博客网 时间:2024/05/18 19:38
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`id` varchar(10) DEFAULT NULL,
`nickname` varchar(5) DEFAULT NULL,
`score` int(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `score` VALUES ('1', '01', '100');
INSERT INTO `score` VALUES ('7', '079', '65');
INSERT INTO `score` VALUES ('2', '023', '99');
INSERT INTO `score` VALUES ('6', '068', '95');
INSERT INTO `score` VALUES ('3', '003', '99');
INSERT INTO `score` VALUES ('4', '045', '96');
INSERT INTO `score` VALUES ('5', '035', '95');
//方式1
select
tmp.id,tmp.nickname,tmp.score,
-- 顺序一直在变大
@j:=@j+1 as j,
-- 前后二次排序值不同时
@k:=(case when @pre_score=tmp.score then @k else @j end) as rank,
@pre_score:=tmp.score as pre_score
from
(
select * from score order by score desc
) tmp,
(select @k :=0,@j:=0, @pre_score:=0) sdcore
//方式3
select
tmp.id,tmp.nickname,tmp.score,
@j:=@j+1 as j,
@k:=(case when @pre_score=tmp.score then @k else @k:=@k+1 end) as rank,
@pre_score:=tmp.score as pre_score
from
(
select * from score order by score desc
) tmp,
(select @k :=0,@j:=0, @pre_score:=0) sdcore
CREATE TABLE `score` (
`id` varchar(10) DEFAULT NULL,
`nickname` varchar(5) DEFAULT NULL,
`score` int(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `score` VALUES ('1', '01', '100');
INSERT INTO `score` VALUES ('7', '079', '65');
INSERT INTO `score` VALUES ('2', '023', '99');
INSERT INTO `score` VALUES ('6', '068', '95');
INSERT INTO `score` VALUES ('3', '003', '99');
INSERT INTO `score` VALUES ('4', '045', '96');
INSERT INTO `score` VALUES ('5', '035', '95');
//方式1
select s.*,@a:=@a+1 as rank from (select * from score order by score desc)s,(select @a:=0)d
//方式2select
tmp.id,tmp.nickname,tmp.score,
-- 顺序一直在变大
@j:=@j+1 as j,
-- 前后二次排序值不同时
@k:=(case when @pre_score=tmp.score then @k else @j end) as rank,
@pre_score:=tmp.score as pre_score
from
(
select * from score order by score desc
) tmp,
(select @k :=0,@j:=0, @pre_score:=0) sdcore
//方式3
select
tmp.id,tmp.nickname,tmp.score,
@j:=@j+1 as j,
@k:=(case when @pre_score=tmp.score then @k else @k:=@k+1 end) as rank,
@pre_score:=tmp.score as pre_score
from
(
select * from score order by score desc
) tmp,
(select @k :=0,@j:=0, @pre_score:=0) sdcore
阅读全文
0 0
- MYSQL 排名方式汇总
- mysql查询成绩排名显示-两种排名方式
- mysql查询成绩排名显示-两种排名方式
- mysql 排名
- MYsql 排名
- mysql 排名
- mysql排名
- PAT排名汇总
- PTA PAT排名汇总
- PAT排名汇总
- PAT排名汇总
- 百度排名方式
- PAT7-07. PAT排名汇总
- PAT排名汇总 (25分)
- 7-21 PAT排名汇总
- 摘抄:mysql 排名
- mysql数据库排名
- mysql 根据字段排名
- 广播机制,动态广播,静态的广播Broadcast,
- js算法集合(一) 水仙花数 及拓展(自幂数的判断)
- Selenium WebDriver中常用到的JavaScript操作
- gitlab runner 的安装
- JavaScript中return的用法详解
- MYSQL 排名方式汇总
- HTML5和CSS3实现3D转换效果 CSS3的3D效果
- 文科生也能搞定的深度学习入门漫画!(上)
- Centos 安装配置jdk
- 整形数组合并
- easyUI常用属性
- mr的shuffle过程
- java基础_java程序结构及简单面向对象
- Git命令整理