如何在mysql中获得第二个最大ID?
请看下面的代码和图片:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `a`
-- ----------------------------
DROP TABLE IF EXISTS `a`;
CREATE TABLE `a` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(30) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of a
-- ----------------------------
INSERT INTO `a` VALUES ('1', 'jimy');
INSERT INTO `a` VALUES ('7', 'khon');
INSERT INTO `a` VALUES ('3', 'tina');
INSERT INTO `a` VALUES ('4', 'kelvin');
INSERT INTO `a` VALUES ('5', 'ricky');
答案 0 :(得分:3)
使用limit
子句:
select *
from a
order by id desc
limit 1, 1
答案 1 :(得分:2)
我喜欢Gordon Linoff的回答,同时提供了我丑陋的条款:
select min(id) as id from
(select * from a order by id desc limit 2) as temp_table;
答案 2 :(得分:0)
试试这个:
select max(id) from a where id != (select max(id) from a)
答案 3 :(得分:0)
获取第二个最高工资的最简单方法第n个工资
select
DISTINCT(salary)
from employee
order by salary desc
limit 1,1
注意:
limit 0,1 - Top max salary
limit 1,1 - Second max salary
limit 2,1 - Third max salary
limit 3,1 - Fourth max salary