mingfish_004 mingfish_004 - 3 years ago 193
MySQL Question

how can i get second max id in mysql?

how can i get second max id in mysql?

see my codes and picture bellow:

enter image description here

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');

Answer Source

Try this:

select max(id) from a where id != (select max(id) from a)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download