各数据库实现 top 操作
sql top 实例
sql server 语法:
SELECT TOP number|percent column_name(s) FROM table_name
例子:
SELECTTOP 2
* FROM employee
sql 语句中 percent 的用法:
从 "employee" 表中选取 50% 的记录。
我们可以使用下面的 SELECT 语句:
SELECT TOP 50 PERCENT
* FROM employee
DB2 中的实现方式为:
语法为:
select column_name(s)
from table_name
fetch first number rows only;
例子为:
select * from employee fetch first 5 rows only; //前5条记录
Oracle top 实现
oracle语法:
SELECT column_name(s) FROM table_name WHERE ROWNUM <= number
例子 1:
SELECT * FROM employee WHERE ROWNUM <= 5
例子 2:
-- 取出早上最先打卡5名员工
SELECT * FROM employee WHERE ROWNUM <= 5 ORDER BY signdate;
或
SELECT * FROM (SELECT * FROM employee ORDER BY signdate)
WHERE ROWNUM <= 5;
-- 取出早上最先打卡6~8名员工
SELECT * FROM (SELECT * FROM employee ORDER BY signdate)
WHERE ROWNUM <= 8
MINUS
SELECT * FROM (SELECT * FROM employee ORDER BY signdate)
WHERE ROWNUM <= 5;
以上是我在数据库中应用的查询方法再此做下总结,希望对有需要的朋友给点帮助。