zoukankan      html  css  js  c++  java
  • mysql 找到topN 大 数字

    找到第N大的数字, 如果没有就显示为NULL

    SELECT DISTINCT(SecondHighestSalary) 
    FROM 
    (SELECT 
    CASE
    WHEN (SELECT (SELECT COUNT(*) FROM (SELECT DISTINCT(salary) FROM employee) t) < 88) THEN NULL
    ELSE (SELECT MIN(salary) FROM (SELECT salary FROM employee a ORDER BY salary DESC LIMIT 0, 88) a) 
    END SecondHighestSalary	
    FROM employee) c
    

    测试脚本如下:

    create table `employee` (
    	`id` int (11),
    	`salary` int (11)
    ); 
    insert into `employee` (`id`, `salary`) values('12','761');
    insert into `employee` (`id`, `salary`) values('6','1037');
    insert into `employee` (`id`, `salary`) values('87','38');
    insert into `employee` (`id`, `salary`) values('1','786');
    insert into `employee` (`id`, `salary`) values('39','1762');
    insert into `employee` (`id`, `salary`) values('65','133');
    insert into `employee` (`id`, `salary`) values('25','2529');
    insert into `employee` (`id`, `salary`) values('96','403');
    insert into `employee` (`id`, `salary`) values('8','4333');
    insert into `employee` (`id`, `salary`) values('11','3164');
    insert into `employee` (`id`, `salary`) values('5','831');
    insert into `employee` (`id`, `salary`) values('18','3579');
    insert into `employee` (`id`, `salary`) values('66','4778');
    insert into `employee` (`id`, `salary`) values('26','4826');
    insert into `employee` (`id`, `salary`) values('82','4957');
    insert into `employee` (`id`, `salary`) values('63','1288');
    insert into `employee` (`id`, `salary`) values('64','2711');
    insert into `employee` (`id`, `salary`) values('73','1842');
    insert into `employee` (`id`, `salary`) values('2','2627');
    insert into `employee` (`id`, `salary`) values('78','1908');
    insert into `employee` (`id`, `salary`) values('22','4667');
    insert into `employee` (`id`, `salary`) values('77','199');
    insert into `employee` (`id`, `salary`) values('69','576');
    insert into `employee` (`id`, `salary`) values('32','2638');
    insert into `employee` (`id`, `salary`) values('79','510');
    insert into `employee` (`id`, `salary`) values('23','622');
    insert into `employee` (`id`, `salary`) values('43','2491');
    insert into `employee` (`id`, `salary`) values('44','4435');
    insert into `employee` (`id`, `salary`) values('28','4552');
    insert into `employee` (`id`, `salary`) values('86','4646');
    insert into `employee` (`id`, `salary`) values('57','451');
    insert into `employee` (`id`, `salary`) values('13','1852');
    insert into `employee` (`id`, `salary`) values('62','2835');
    insert into `employee` (`id`, `salary`) values('72','544');
    insert into `employee` (`id`, `salary`) values('37','2896');
    insert into `employee` (`id`, `salary`) values('89','4699');
    insert into `employee` (`id`, `salary`) values('71','220');
    insert into `employee` (`id`, `salary`) values('46','2333');
    insert into `employee` (`id`, `salary`) values('95','2261');
    insert into `employee` (`id`, `salary`) values('48','3210');
    insert into `employee` (`id`, `salary`) values('17','398');
    insert into `employee` (`id`, `salary`) values('20','57');
    insert into `employee` (`id`, `salary`) values('74','176');
    insert into `employee` (`id`, `salary`) values('54','4272');
    insert into `employee` (`id`, `salary`) values('35','2172');
    insert into `employee` (`id`, `salary`) values('84','4686');
    insert into `employee` (`id`, `salary`) values('42','2595');
    insert into `employee` (`id`, `salary`) values('76','3631');
    insert into `employee` (`id`, `salary`) values('45','4022');
    insert into `employee` (`id`, `salary`) values('68','3248');
    insert into `employee` (`id`, `salary`) values('40','571');
    insert into `employee` (`id`, `salary`) values('14','4094');
    insert into `employee` (`id`, `salary`) values('51','3272');
    insert into `employee` (`id`, `salary`) values('97','2057');
    insert into `employee` (`id`, `salary`) values('30','2505');
    insert into `employee` (`id`, `salary`) values('88','3805');
    insert into `employee` (`id`, `salary`) values('55','4926');
    insert into `employee` (`id`, `salary`) values('81','3423');
    insert into `employee` (`id`, `salary`) values('7','811');
    insert into `employee` (`id`, `salary`) values('52','4664');
    insert into `employee` (`id`, `salary`) values('33','1842');
    insert into `employee` (`id`, `salary`) values('41','4645');
    insert into `employee` (`id`, `salary`) values('50','1490');
    insert into `employee` (`id`, `salary`) values('24','3361');
    insert into `employee` (`id`, `salary`) values('59','4504');
    insert into `employee` (`id`, `salary`) values('99','805');
    insert into `employee` (`id`, `salary`) values('67','4561');
    insert into `employee` (`id`, `salary`) values('61','4641');
    insert into `employee` (`id`, `salary`) values('38','2542');
    insert into `employee` (`id`, `salary`) values('94','4361');
    insert into `employee` (`id`, `salary`) values('15','1652');
    insert into `employee` (`id`, `salary`) values('70','1903');
    insert into `employee` (`id`, `salary`) values('10','2109');
    insert into `employee` (`id`, `salary`) values('49','1428');
    insert into `employee` (`id`, `salary`) values('29','558');
    insert into `employee` (`id`, `salary`) values('60','708');
    insert into `employee` (`id`, `salary`) values('3','1717');
    insert into `employee` (`id`, `salary`) values('0','3162');
    insert into `employee` (`id`, `salary`) values('75','1450');
    insert into `employee` (`id`, `salary`) values('83','2333');
    insert into `employee` (`id`, `salary`) values('47','455');
    insert into `employee` (`id`, `salary`) values('9','2360');
    insert into `employee` (`id`, `salary`) values('56','1570');
    insert into `employee` (`id`, `salary`) values('80','1677');
    insert into `employee` (`id`, `salary`) values('27','508');
    insert into `employee` (`id`, `salary`) values('21','3994');
    insert into `employee` (`id`, `salary`) values('85','2276');
    insert into `employee` (`id`, `salary`) values('91','1606');
    insert into `employee` (`id`, `salary`) values('90','2269');
    insert into `employee` (`id`, `salary`) values('58','1954');
    insert into `employee` (`id`, `salary`) values('19','774');
    insert into `employee` (`id`, `salary`) values('4','655');
    insert into `employee` (`id`, `salary`) values('92','3274');
    insert into `employee` (`id`, `salary`) values('16','333');
    insert into `employee` (`id`, `salary`) values('36','1744');
    insert into `employee` (`id`, `salary`) values('53','2262');
    insert into `employee` (`id`, `salary`) values('93','3887');
    insert into `employee` (`id`, `salary`) values('31','3521');
    insert into `employee` (`id`, `salary`) values('34','1415');
    insert into `employee` (`id`, `salary`) values('98','2490');
    

    结果是: 510

  • 相关阅读:
    从图片中提取html格式的布局
    javascript语法
    2015 9月2日 工作计划与执行
    2015 9月1日 工作计划与执行
    支付模块结构设计
    ubuntu下的pycharm4中文路径乱码
    2015 8月31 工作计划与执行
    25个git进阶技巧 2015-05-12 16:04 34人阅读 评论(0) 收藏
    Model/View框架总体架构 分类: QT学习实践 2015-05-11 22:05 34人阅读 评论(0) 收藏
    用Dom处理XML文件 分类: QT学习实践 2015-05-11 21:16 30人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/12753505.html
Copyright © 2011-2022 走看看