1.SQL SERVER 2000 是用先逆序再正序的方法
select top 3 * from (select top 9 * from mvc_book order by bid) as s order by s.bid desc [第6,5,4條]
select * from (select top 3 * from (select top 6 * from mvc_book order by bid) as s order by s.bid desc) as s1 order by s1.bid [第4,5,6條]
select top 6 * from mvc_book where (bid not in (select top 2 bid from mvc_book))[第3,4,5,6,7,8條]
sql2005
WITH, ROW_NUMBER (行數)and OVER
這對SQL Server 2005來說非常新鮮並且看上去非常有用。下面一個例子顯示從一個結果集得到20至19條記錄。剛開始有一點驚奇,但是瀏覽了查詢器後發現它是如此簡單。
With Cust AS
( SELECT CustomerID, CompanyName,
ROW_NUMBER() OVER (order by CompanyName) as RowNumber
FROM Customers )
select *
from Cust
Where RowNumber Between 20 and 30
SQL Server 2005的WITH指定了一個臨時命名的結果,很像SQL Server以前版本中的暫存資料表。但是,輸入部分是ROW_NUMBER和OVER聲明,它根據公司的名稱在每組中建立行數。這就像通過命令條文向暫存資料表添加一個身份種子。
我希望你贊成我的看法。如果不,運行代碼並查看結果集。對大表來說速度真的非常快; 表的速度超過250,000條記錄,對此我留下了深刻的印象。
2.ORACLE 用Minus和Rownum來實現 (Minus 減去 Union 聯合 Intersect 返回相同的記錄集)
select * from t_service_vnet_send where rownum <= 15 MINUS select * from t_service_vnet_send where rownum <= 10;
或
select * from (select rownum no,id,age,name from loaddata where rownum <= 3 ) where no >= 2;
3.DB2
select * from (select ROW_NUMBER() over() as a, org.* from org) as temp where a>=n1 and a<=n2
4.MYSQL
select * from tablename limit m,n;
兩個有用的oracle資料庫運算:intersect和minus運算
intersect運算
返回查詢結果中相同的部分
exp:各個部門中有哪些相同的工種
select job
from account
intersect
select job
from research
intersect
select job
from sales;
minus運算
返回在第一個查詢結果中與第二個查詢結果不相同的那部分行記錄。
有哪些工種在財會部中有,而在銷售部中沒有?
exp:select job from account
minus
select job from sales;