假设我的数据库表如下name      date    month    subject     score
小明        2014      1            数学          80
小明        2014       1          语文          75
小明        2014       1          英语         70
小明        2014      2            数学         100
小明        2014       2         语文          75
小明        2014      2          英语         70
小红        2014      1            数学          80
小红         2014       1          语文          75
小红       2014       1          英语         70
小红        2014      2            数学         60
小红         2014       2         语文          75
小红         2014      2          英语         90我怎么能够得出每位同学在每年每月中某门成绩的最大值?即结果是:
小明        2014      1            数学          80
小明        2014      2            数学         100
小红        2014      1            数学          80
小红         2014      2          英语         90

解决方案 »

  1.   

    select A.name,A.date,A.month,A.subject,A.score
    from table A where not exists 
    (select 1 from table B where A.name = B.name
    and A.date = B.date and A.month = B.month and A.score < B.score)
      

  2.   

    select *
    from 假设我的数据库表 a
    where not exists (select 1 from 假设我的数据库表 where date=a.date and month=a.month and name=a.name and score>a.score)
      

  3.   

    参考下贴中的多种方法http://blog.csdn.net/acmain_chm/article/details/4126306
    [征集]分组取最大N条记录方法征集,及散分....
      

  4.   

    这表没设主键么……
    select b.name,b.date,b.month,b.subject,b.score
    from (select name,date,month,max(score) as score  from tbl group by name,date,month ) a
    inner join tbl b
    on a.name = b.name
    and a.date = b.date
    and a.month = b.month
    and a.score = b.score
      

  5.   

    select
        name,date,month,subject,max(score)
    from 
        table
    group by 
        concat(name,month)
    试试,没环境,没法测试!
      

  6.   


    没记错的话,这个写法在mysql下会有问题
    出来的subject很可能不是最高分对应的subject
      

  7.   


    SELECT NAME,DATE,MONTH,SUBJECT,MAX(score) 
    FROM tablename t 
    GROUP BY NAME,DATE,MONTH,SUBJECT;