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条记录。刚开始有一点惊奇,但是浏览了查询器后发现它是如此简单。
( 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;