【问题标题】:paging and ordering a MS Access query分页和排序 MS Access 查询
【发布时间】:2014-07-24 16:49:52
【问题描述】:

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

 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 到 g 开头......然后在第 2 页它又回来了以 c .... 开头的名称,依此类推

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

有什么解决方法吗?

【问题讨论】:

标签: database sorting ms-access pagination


【解决方案1】:

也试试这个方法:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2014-01-08
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多