資料庫讀取前幾條記錄的sql語句大全

2021-03-07 17:12:41 字數 2708 閱讀 8157

1樓:檀香透窗櫺

取前10條記錄的sql語句寫法:

1、access:

select top (10) * from table1 where 1=1

2、db2:

select column from table where 1=1 fetch first 10 rows only

3、mysql:

select * from table1 where 1=1 limit 10

4、sql server:

讀取前10條:select top (10) * from table1 where 1=1

讀取後10條:select top (10) * from table1 order by id desc

5、oracle:

select * from table1 where rownum<=10

擴充套件資料

pl/sql的流程控制語句,包括如下三類:

l 控制語句: if 語句

l 迴圈語句: loop語句, exit語句

l 順序語句: goto語句, null語句

條件語句:

if 《布林表示式》 then

pl/sql 和 sql語句

end if;

if 《布林表示式》 then

pl/sql 和 sql語句

else

其它語句

end if;

if 《布林表示式》 then

pl/sql 和 sql語句

elsif < 其它布林表示式》 then

其它語句

elsif < 其它布林表示式》 then

其它語句

else

其它語句

end if;

2樓:匿名使用者

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

vb怎麼讀取acceess資料庫中的資料並顯示在介面上

開啟vb程式,點檔案 新建工程 資料工程,在視窗新增一個command控制元件和一個datagrid控制元件。插入如下 private sub command1 click dim mycon as new adodb.connectiondim myrt as new adodb.recordse...

C怎麼取出資料庫的記錄

把這段 sqldatareader dreader cmd.executereader 換成 sqldataadapter sda new sqldataadapter cmd datatable dt new datatable sda.fill dt return dt 這個返回的是存放資料的表...

sql如何查詢空值的欄位,sql資料庫查詢中,空值查詢條件怎麼寫?

sql查詢空值的欄位寫法 select a.欄位 from student a where a.欄位 like student為表名 查詢類似空值的寫法 1 查詢名稱有退格鍵 select from t bd item info where charindex char 8 item name 0 ...