1. oracle資料庫select * from tablename where rownum <= n
2. infomix資料庫
select first n * from tablename
3. db2資料庫
select *
from (select * row_number() over() as rownum from tablename)
where rownum <= n
或者select column from tablename fetch first n rows only
4. sql server資料庫
select top n * from tablename
5. sybase資料庫
set rowcount n
goselect * from tablename
6. mysql資料庫
select * from tablename limit n
7. foxpro資料庫
select * top n from tablename order by column
以下示例從表 [tablename] 中讀取符合查詢條件的前10條記錄的sql語句
1.access
select top (10) * from [tablename] where [query condition]
1.1 帶order by的查詢限制
access中對select top的語句支援有限,如果要在查詢top語句的後面使用order by,則order by排序字段必須是無重複值,如果有重複值的話,那麼這個top很可能會失效,會返回所有記錄。
解決辦法:在order by 最後面加入主鍵id,如:
select top 10 from [tablename] order by 排序欄位1,id
1.2 帶子查詢的示例
假如id是表[tablename]的主鍵,以下語句期望返回三條記錄,但結果返回4條記錄
select top 3 * from [tablename] where id in(是個子查詢,結果比如為1,2,3,4)
解決辦法
select top 3 * from [tablename] where id in(是個子查詢,結果比如為1,2,3,4) order by id
2 db2
select column from [tablename] where [query condition] fetch first 10 rows only
3 mysql
select * from [tablename] where [query condition] limit 10
4 sql server
4.1 讀取前10條
select top (10) * from [tablename] where [query condition]
4.2 讀取後10條
select top (10) * from [tablename] order by id desc
4.3 按照某個排序,第5到10這幾個記錄
select top 6 * from [tablename] where id not in(select top 4 id from [tablename])
5 oracle
select * from [tablename] where rownum<=10
各種資料庫查詢前幾條資料的方法
sql在不同資料庫查詢前幾條資料 關鍵字 sql 前幾條結果 sql在不同資料庫查詢前幾條資料 1.oracle select from table1 where rownum n select from stu info where rownum 10 查詢學生資訊表的前10條資料 hql fro...
資料庫 讀取前幾條資料
1.oracle資料庫 select from tablename where rownum n 2.infomix資料庫 select first n from tablename 3.db2資料庫 select from select row number over as rownum from...
分組查詢前幾條資料
create table t id varchar 3 gid int,author varchar 29 title varchar 39 date datetime insert into tselect 001 1,鄒建 深入淺出sqlserver2005開發管理與應用例項 2008 05 1...