how can i get second max id in mysql?
see my codes and picture bellow:
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');
Use the limit
clause:
select *
from a
order by id desc
limit 1, 1
I like Gordon Linoff's answer, while providing my ugly clause:
select min(id) as id from
(select * from a order by id desc limit 2) as temp_table;
尝试这个:
select max(id) from a where id != (select max(id) from a)
Simplest way to fetch second max salary & nth salary
select
DISTINCT(salary)
from employee
order by salary desc
limit 1,1
Note:
limit 0,1 - Top max salary
limit 1,1 - Second max salary
limit 2,1 - Third max salary
limit 3,1 - Fourth max salary
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.