日期:2014-05-18 浏览次数:20653 次
建议你提供详细的资料: 例如表的结构,表之间的关系,测试数据,相关算法及需要的结果。 这样有助于我们理解你的意思,更主要的是能尽快让你获得答案或解决问题的方法。
------解决方案--------------------
将你的查询条件字段上建索引(最好能建个包含索引) 比如 create index idx_name on table_name include (NAME,MAXENGLISH,MAXMATH,MAXCHINESE)
------解决方案--------------------
select NAME,MAX(ENGLISH) as MAXENGLISH ,MAX(MATH)as MAXMATH ,MAX(CHINESE) as MAXCHINESE FROM T_STUDENT S Inner join T_SCORE SC on T.ID=SC.STUDENTID GROUP BY NAME
------解决方案--------------------
select NAME,max(ENGLISH) as MAXENGLISH,max(MATH ) as MAXMATH ,
max(HINESE) as MAXCHINESE from T_STUDENT left join T_SCORE
on T_STUDENT.id=T_SCORE.STUDENTID
------解决方案--------------------
索引会加快速度
------解决方案--------------------
select NAME,max(ENGLISH) as MAXENGLISH,max(MATH ) as MAXMATH ,
max(HINESE) as MAXCHINESE from T_STUDENT left join T_SCORE
on T_STUDENT.id=T_SCORE.STUDENTID
where date ......
group by NAME
------解决方案--------------------
用连接吧,不要用子查询
select A.NAME,B.MAXENGLISH,B.MAXMATH,B.MAXCHINESE
from T_STUDENT A
inner join
(
select STUDENTID,max(ENGLISH) as MAXENGLISH,max(MATH) as MATH,max(CHINESE) as CHINESE
from T_SCORE
group by STUDENTID
) B on B.STUDENTID = A.Id
------解决方案--------------------
select m.* , n.* from T_STUDENT m ,T_SCORE n where m.STUDENTID = n.STUDENTID and n.score = (select max(score) from T_SCORE where 科目 = n.科目 and STUDENTID =nSTUDENTID )
------解决方案--------------------
至于连接还是子查询好是根据你自己的实际情况来定夺的,要看执行计划,不是凭空想象的。