mysql 排序 rownum_Mysql实现Rownum()排序后根据条件获取名次

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:08   1034   0

初始化表结构

DROP TABLE IF EXISTS `data`;

CREATE TABLE `data` (

`dates` varchar(255) CHARACTER SET utf8 DEFAULT NULL,

`id` int(11) DEFAULT NULL,

`result` varchar(255) CHARACTER SET utf8 DEFAULT NULL

);

INSERT INTO `data` (`dates`, `id`, `result`) VALUES ('2015109101', 1, '胜');

INSERT INTO `data` (`dates`, `id`, `result`) VALUES ('2015110101', 2, '负');

INSERT INTO `data` (`dates`, `id`, `result`) VALUES ('2015109101', 3, '负');

INSERT INTO `data` (`dates`, `id`, `result`) VALUES ('2015109101', 4, '胜');

INSERT INTO `data` (`dates`, `id`, `result`) VALUES ('2015110101', 5, '胜');

INSERT INTO `data` (`dates`, `id`, `result`) VALUES ('2015109101', 6, '负');

INSERT INTO `data` (`dates`, `id`, `result`) VALUES ('2015109101', 7, '胜');

INSERT INTO `data` (`dates`, `id`, `result`) VALUES ('2015110101', 8, '负');

排序

select @rownum:=@rownum+1 AS rownum,id,dates

from

`data`,(SELECT @rownum:=0) r

ORDER BY dates;

结果

125539_0.jpg

条件查询

SELECT rownum,id

from

(select @rownum:=@rownum+1 AS rownum,id,dates

from

`data`,(SELECT @rownum:=0) r

ORDER BY dates)b

WHERE id =2;

结果

125539_1.jpg

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP