zoukankan      html  css  js  c++  java
  • sql求倒数第二大的数,效率不高,但写法新颖

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication9
    {
        class Program
        {
            static void Main(string[] args)
            {
    // sql如下,我把它翻译成代码,加深理解
    // SELECT MAX(id)FROM sys_menu E1 // WHERE // (SELECT COUNT(DISTINCT(E2.id)) FROM sys_menu E2 // WHERE E2.id > E1.id // ) = 1; --9990 List<int> list = new List<int> { 1, 2, 3, 55, 66 }; int max = 0; foreach (var id1 in list) { int count = 0; var dic = new Dictionary<int, int>(); foreach (var id2 in list) { if (!dic.ContainsKey(id2)) { if (id2> id1) { count++; } dic[id2] = 1; } } //上面的代码说明id2>id1那么id1就不可能是最大的,并且比id1大的要求只能有一个,那么说过成语吧一人之下,万人之上 (注意这个至少要2个元素才有意义) //所以也说明上面的sql有个陷阱SELECT MAX(id) 改为 select id也是可以的, 但其实max也是为了没有记录的时候返回一个空值 if (count != 1) { continue; } if (max == 0) { max = id1; } else if (id1> max) { max = id1; } } Console.WriteLine(max); Console.Read(); } } }
  • 相关阅读:
    老白关于rac性能调优的建议
    缓存融合(Cache Fusion)介绍
    交易所行情报盘程序配置
    SQLServer 断开指定会话
    缓存融合技术介绍
    机器学习参考书籍
    寒霜浸铁甲,英雄见白头
    希腊字母表
    windows命令
    大空头
  • 原文地址:https://www.cnblogs.com/kexb/p/9265451.html
Copyright © 2011-2022 走看看