• MySQl查询各科成绩前三名


    创建表

    create table student ( 
        name varchar(20) , 
        lesson varchar(20), 
        mark float 
    ) ;

    插入数据

    insert into student values('john','Math',60); 
    insert into student values('john','Eng',50); 
    insert into student values('john','HIstory',56); 
    
    insert into student values('Mike','Eng',51); 
    insert into student values('Mike','Math',59); 
    insert into student values('Mike','HIstory',55); 
    
    insert into student values('Mark','Eng',71); 
    insert into student values('Mark','Math',89); 
    insert into student values('Mark','HIstory',95); 
    
    insert into student values('张三','Eng',61); 
    insert into student values('张三','Math',79); 
    insert into student values('张三','HIstory',85); 
    
    insert into student values('李明','Eng',51); 
    insert into student values('李明','Math',69); 
    insert into student values('李明','HIstory',95);

    查询

    #方法一
    SELECT T1.*
    FROM student T1
    LEFT JOIN (
    SELECT DISTINCT lesson,mark
    FROM student) T2 ON T1.lesson = T2.lesson AND T1.mark <= T2.mark GROUP BY name,lesson,mark 
    HAVING COUNT(1) <= 3  ORDER BY lesson,mark DESC;
    
    
    #方法二
    SELECT s1.*
    FROM student s1
    WHERE (
    SELECT COUNT(1)
    FROM student s2
    WHERE s1.lesson=s2.lesson AND s1.mark<s2.mark)<3
    ORDER BY s1.lesson,s1.mark DESC;
  • 相关阅读:
    纸牌博弈问题
    Eureka Server 实现在线扩容
    设计模式学习(二):单例模式
    最大的观影时间问题
    拼凑硬币问题
    泡咖啡问题
    设计模式学习(五):原型模式
    最长公共子序列问题
    设计模式学习(六):代理模式
    经典背包系列问题
  • 原文地址:https://www.cnblogs.com/boye169/p/15852143.html
Copyright © 2020-2023  润新知