MySql排名查询

1.新建一张成绩表

-- 新建成绩表

CREATE TABLE IF NOT EXISTS `score` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(32) NOT NULL DEFAULT '',

  `scores` int(11) NOT NULL DEFAULT '0',

  PRIMARY KEY (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;



-- 插入学生成绩

INSERT INTO `score` (`id`, `name`, `scores`) VALUES

(1, 'a', 97),

(2, 'b', 90),

(3, 'c', 98),

(4, 'd', 97),

(5, 'e', 66);

2.查询成绩排名

select * from

(

    select (select count(id)+1  from score where scores>a.scores) as mc,a.name,a.scores

    from score a

) b where b.mc<=10 

order by b.mc;

查询结果如下:

MySql排名查询

你可能感兴趣的:(mysql)