zoukankan      html  css  js  c++  java
  • mysql实现topN top1

      有时会碰到一些需求,查询分组后的最大值,最小值所在的整行记录或者分组后的top n行的记录,像在hive中是有窗口函数的,可以通过它们来实现,但是MySQL没有这些函数,可通过下面的方法来实现

    1、准备

    create table `test1` (
      `id` int(11) not null auto_increment,
      `name` varchar(20) default null,
      `course` varchar(20) default null,
      `score` int(11) default null,
      primary key (`id`)
    ) engine=innodb auto_increment=10 default charset=utf8
    
    
    insert into test1(name,course,score)
    values 
    ('张三','语文',80),
    ('李四','语文',90),
    ('王五','语文',93),
    ('张三','数学',77),
    ('李四','数学',68),
    ('王五','数学',99),
    ('张三','英语',90),
    ('李四','英语',50),
    ('王五','英语',89);

    2、TOP 1

      需求:查询每门课程分数最高的学生以及成绩

      实现方法:可以通过自连接、子查询来实现,如下

      a、自连接实现

    select a.name,a.course,a.score 
    from test1 a  join (select course,max(score) score from test1 group by course) b  
    on a.course=b.course and a.score=b.score;

      执行效果如下

            

      b、子查询实现

    select name,course,score 
    from test1 a  
    where score=(select max(score) from test1 where a.course=test1.course);

      执行效果如下

            

      也可以用下面这个子查询实现

    select name,course,score 
    from test1 a 
    where not exists(select 1 from test1 where a.course=test1.course and a.score < test1.score);

      执行效果如下

          

    3、TOP N

      需求:查询每门课程前两名的学生以及成绩

      实现方式:使用union all、自身左连接、子查询、用户变量等方式实现

      a、使用union all实现

    (select name,course,score from test1 where course='语文' order by score desc limit 2)
    union all
    (select name,course,score from test1 where course='数学' order by score desc limit 2)
    union all
    (select name,course,score from test1 where course='英语' order by score desc limit 2);

      执行效果如下

            

      b、使用自身左连接

    select a.name,a.course,a.score 
    from test1 a left join test1 b on a.course=b.course and a.score<b.score
    group by a.name,a.course,a.score
    having count(b.id)<2
    order by a.course,a.score desc;

      执行效果如下

            

      c、使用子查询

    select *
    from test1 a
    where 2>(select count(*) from test1 where course=a.course and score>a.score)
    order by a.course,a.score desc;

      执行效果如下

            

      d、使用用户变量

    set @num := 0, @course := '';
    
    select name, course, score
    from (
    select name, course, score,
    @num := if(@course = course, @num + 1, 1) as row_number,
    @course := course as dummy
    from test1
    order by course, score desc
    ) as x where x.row_number <= 2;

      执行效果如下

     

    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】。
    如果,您希望更容易地发现我的新博客,不妨点击一下左下角的【关注我】。
    如果,您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客,我是【刘超★ljc】。

    本文版权归作者,禁止转载,否则保留追究法律责任的权利。

  • 相关阅读:
    【脑图】iOS的Crash分类和捕获
    Ruby03
    Ruby02
    Ruby01
    如何快速把一个十进制数转换为二进制?
    iOS
    互联网协议基本知识
    XCBB
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/codeOfLife/p/7410620.html
Copyright © 2011-2022 走看看