簡體   English   中英

我怎樣才能在MySQL中獲得第二個最大ID?

[英]how can i get second max id in mysql?

我怎樣才能在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');

使用limit子句:

select *
from a
order by id desc
limit 1, 1

我喜歡Gordon Linoff的回答,同時提供了我的丑陋條款:

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)

獲取第二個最高薪水和第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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM