开发进行时...

crazy coder

Avatar

查询前几条记录SQL在不同数据库中的用法


1. ORACLE

SELECT * FROM TABLE1 WHERE ROWNUM<=N

2. INFORMIX

SELECT FIRST N * FROM TABLE1

3. DB2

SELECT * FROM (SELECT T.*, ROW_NUMBER() OVER(ORDER BY COL1 DESC) AS ROWNUM FROM T) AS T WHERE ROWNUM<=N
或者
SELECT COLUMN FROM TABLE FETCH FIRST N ROWS ONLY

4. SQL SERVER

SELECT TOP N * FROM TABLE1

5. SYBASE

SET ROWCOUNT N GOSELECT * FROM TABLE1

6. MYSQL

SELECT * FROM TABLE1 LIMIT N

7. FOXPRO

SELECT * TOP N FROM TABLE ORDER BY COLUMN


不过个人认为oracle的rownum查询不太好,喜欢使用rank函数进行排序和部分记录的选择(好比前几条,中间几条)。

评论已关闭