分页和排序MS Access查询

时间:2014-07-23 18:53:36

标签: database sorting ms-access pagination

我有以下MS ACCESS查询,我希望它返回按名称排序的结果,“paged”通过“伪造”rownumber

 select * from (SELECT *
 FROM (SELECT  
        s.name as SHolderCategory,
        c1.id,
        c1.fmember,
        c1.link,
        m.name as category,
        c1.name,
        c1.address1,
        c1.address2,
        c1.city,
        c1.state,
        c1.zip, 
        (SELECT COUNT(c2.id) FROM orgs AS c2 WHERE c2.id <= c1.id) AS rownumber                
 FROM 
((orgs AS c1 inner join membershipcls m on m.Id = c1.mClassID)  
 inner join SHolderscategories s on s.Id = c1.SHolderCategoryID
 ) 
 where c1.active = 1) 
  order by c1.name)
WHERE  rownumber > 20 AND rownumber <=40

这里的问题是排序是在执行分页的where子句之前完成的。 所以它最终一次排序一个页面,而不是整个结果集然后分页...所以结果是错误的,因为在第1页我的名字以a开头...然后在第2页它回来了以c ....开头的名字等等

当我尝试获取订单子句以便查询首先执行分页时...先生ACCESS很生气!并告诉我这是一个复杂的查询!!!!

为此做任何解决方法?

1 个答案:

答案 0 :(得分:2)

也尝试这种方法:

SELECT * FROM
(
    SELECT TOP 20 *
    FROM 
    (
        SELECT TOP 40
            s.name as SHolderCategory,
            c1.id,
            c1.fmember,
            c1.link,
            m.name as category,
            c1.name,
            c1.address1,
            c1.address2,
            c1.city,
            c1.state,
            c1.zip
        FROM 
            orgs AS c1 
            inner join membershipcls m on m.Id = c1.mClassID
            inner join SHolderscategories s on s.Id = c1.SHolderCategoryID
        WHERE c1.active = 1
        ORDER BY c1.name
    ) o 
    ORDER BY o.name DESC
) f ORDER BY f.name