从100万条记录中的到 成绩最高的记录


问题分析:

要从一张表中找到成绩最高的记录并不难,有很多种办法,最简单的就是利用TOP 1 


select top 1 * from student order by score desc


top

TOP 子句用于规定要返回的记录的数目。

对于拥有数千条记录的大型表来说,TOP 子句是非常有用的。

注释:并非所有的数据库系统都支持 TOP 子句。

SQL Server 的语法:

SELECT TOP number|percent column_name(s)
FROM table_name



MySQL 和 Oracle 中的 SQL SELECT TOP 是等价的

MySQL 语法

SELECT column_name(s)
FROM table_name
LIMIT number


但是这种方法有一点小问题,就是如果成绩最高的有两个人的话只能查出其中的一个。

对上面的方法进行改进: 


select top 1 with ties * from student order by score desc (sql server特有,mysql没有with ties)


或用子查询的方式:


select * from student where score =(

select max(score) from student)

select * from student where score = (select top 1 score from student order by score desc)


=================成功的分割线======================

但是这个题目成功与否不在于能否查询出成绩最高的记录,而在于用最快的速度查询出来。经过测试从100万条记录中找到成绩最高的记录如果使用第一个方法需要1秒多,