Home > Database > Mysql Tutorial > TOP_MySQL

TOP_MySQL

WBOY
Release: 2016-05-30 17:09:47
Original
1220 people have browsed it

TOP 关键字结合SELECT 查询使用,显示前几条的记录

 

常有两种使用方式:

 

一、选择前N条记录

 

示例1:查询出病假时间较少的前10个职员的信息

 

1 USE AdventureWorks2014

2 SELECT TOP 10 [SickLeaveHours]

3 FROM [HumanResources].[Employee]

4 ORDER BY [SickLeaveHours] 

查询结果:

 

  SickLeaveHours

 

 

 1 20

 2 20

 3 20

 4 20

 5 20

 6 20

 7 21

 8 21

 9 21

10 21

 

 

 

二、选择前N%的记录

 

示例2:查询病假时间较少的前百分之2个职员的信息

 

1 USE AdventureWorks2014

2 SELECT TOP 2 PERCENT [SickLeaveHours]

3 FROM [HumanResources].[Employee]

4 ORDER BY [SickLeaveHours]

查询结果:

 

  SickLeaveHours

 

1 20

2 20

3 20

4 20

5 20

6 20

 

 

特别的,WITH TIES 关键字的使用:

 

一般包含TOP的查询显示规定数量的结果,TOP N 只显示N条记录,对于TOP N PERCENT 只显示总数N%的结果。

 

但是包含WITH TIES 的查询在先显示规定数量的结果后,与结果最后一条记录的排序字段值相同的记录也被显示

 

示例3:在实例1的基础上加上WITH TIES 关键字

 

1 USE AdventureWorks2014

2 SELECT TOP 10 WITH TIES [SickLeaveHours]

3 FROM [HumanResources].[Employee]

4 ORDER BY [SickLeaveHours]

查询结果:

 

   SickLeaveHours

 

 

 1 20

 2 20

 3 20

 4 20

 5 20

 6 20

 7 21

 8 21

 9 21

10 21

11 21

12 21

Related labels:
top
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template