读书人

sql分页怎么用exists替换not in?

发布时间: 2012-05-02 15:36:04 作者: rapoo

sql分页如何用exists替换not in???

SQL code
SELECT TOP 5 *FROM tableWHERE (id NOT IN          (SELECT TOP 10 id         FROM table))

上面是段分页用的sql,如何用exists替换not in

[解决办法]
SQL code
goif OBJECT_ID('test')is not nulldrop table testgocreate table test(id int identity(1,1),value int)godeclare @b intset @b=1while @b<100begininsert testselect @bset @b=@b+1endSELECT TOP 5 *FROM testWHERE (id NOT IN          (SELECT TOP 15 id         FROM test))/*id    value16    1617    1718    1819    1920    20         */         SELECT TOP 5 *FROM test bWHERE not exists(select 1 from(select top 15 id from test)a where b.id<=a.id)/*id    value16    1617    1718    1819    1920    20*/ 

读书人网 >SQL Server

热点推荐