Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-08 08:50:36
Original
2527 people have browsed it

初始化表结构

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;

结果

 

这里写图片描述

 

条件查询

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;

结果

 

这里写图片描述
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!