假设我的数据库表如下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
小明 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
解决方案 »
- err日志提示 'proxies_priv' entry '@ [email protected]' ig
- mysql5.1误删除innodb表文件时mysql无法启动怎么办
- mysql数据库 enum类型问题
- 请教一个SQL语句
- mysql中function的问题
- ERROR 1146: Table 'mydb.tb_config' doesn't exist
- 导入txt文本数据到数据库中的表出错,敬请指点。
- 存储过程加一个未被使用的参数导致结果不同?
- pt-table-checksum 无结果输出,日志提示此数据库为系统库,被过滤,请问如何解决
- MySQL数据六个字段24W数据 select × 需要10秒 请教可能是哪些问题
- MySQL查询中嵌套查询的问题
- MySQL创建用户不能登录问题,如图
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)
from 假设我的数据库表 a
where not exists (select 1 from 假设我的数据库表 where date=a.date and month=a.month and name=a.name and score>a.score)
[征集]分组取最大N条记录方法征集,及散分....
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
name,date,month,subject,max(score)
from
table
group by
concat(name,month)
试试,没环境,没法测试!
没记错的话,这个写法在mysql下会有问题
出来的subject很可能不是最高分对应的subject
SELECT NAME,DATE,MONTH,SUBJECT,MAX(score)
FROM tablename t
GROUP BY NAME,DATE,MONTH,SUBJECT;