1. 程式人生 > >資料庫只取前多少行記錄sql

資料庫只取前多少行記錄sql

資料庫中只取前多少行記錄的sql語句,下面是一些例子,本人經常用到的是Oracle和db2

1. ORACLE (親測
SELECT * FROM TABLENAME WHERE ROWNUM<=N



2. INFORMIX
    SELECT FIRST N * FROM TABLENAME


3. DB2 (親測
SELECT COLUMN FROM TABLENAME FETCH FIRST N ROWS ONLY


4. SQL SERVER
SELECT TOP N * FROM TABLENAME


5. SYBASE
SET ROWCOUNT N
GO
SELECT * FROM TABLENAME


6. MYSQL
SELECT * FROM TABLENAME LIMIT N


7. FOXPRO
SELECT * TOP N FROM TABLENAME ORDER BY COLUMN

8.postgres查詢前幾條記錄SQL
SELECT * FROM TABLENAME LIMIT

hibernate查詢記錄的前10條記錄(注意:order by newid()是指每次排序都是隨即的)

String hql="select * from tablename t where t.id1=? and t.id2=? order by newid()";
//建立查詢物件
Query query = this.getSession().createQuery(hql);
//對查詢語句進行賦值
query.setParameter(0,id1);
query.setParameter(1,id2);
//設定起始行數和最大查詢行數
query.setFirstResult(0);//從第0條開始取

query.setMaxResults(10);//取十條記錄

return query.list();