简体   繁体   English

分页结果集时从mysql子查询返回最后一行

[英]Returning the last row from a mysql subquery when paginating a result set

I'm using the following mysql query to create a pagination array -- for a list of documents -- in the form "Ab-Cf | Cg-Le | Li-Ru " etc... 我正在使用以下mysql查询创建分页数组-用于文档列表-形式为“ Ab-Cf | Cg-Le | Li-Ru”等。

The subquery 'Subquery' selects the entire list of documents, and is variable depending on the user privileges, requirements etc -- so I'm trying to avoid altering that part of the query (and have used a simplified version here). 子查询“子查询”选择整个文档列表,并且根据用户权限,要求等而可变-因此,我试图避免更改查询的那部分(并在此处使用了简化版本)。

I'm then selecting the first and last row of each page range -- ie, the 1st and 10th row, the 11th and 20th row etc., determined by $num_rows_per_page. 然后,我选择每个页面范围的第一行和最后一行-即第一行和第十行,第11行和第20行等,由$ num_rows_per_page确定。

SELECT * FROM 
  ( 
  SELECT @row := @row + 1 AS `rownum`, `sort_field` FROM 
    ( 
    SELECT @row := 0 ) r, ( 
                          SELECT D.`id`, D.`display_name` as display_field, 
                          D.`sort_name` as sort_field 
                          FROM Document D ORDER BY `sort_field` ASC 
                          ) Subquery 
     ) Sorted 
     WHERE rownum % $num_rows_per_page = 1 OR rownum % $num_rows_per_page = 0

This is working just fine, and gives me a result set like: 这工作得很好,并给我一个结果集,如:

+---------+-----------------------------------+
| rownum  | index_field                       |
+---------+-----------------------------------+
|       1 | Alternaria humicola               | 
|      10 | Anoplophora chinensis             | 
|      11 | Atherigona soccata                | 
|      20 | Carlavirus RCVMV                  | 
|      21 | Cephus pygmaeus                   | 
|      30 | Colletotrichum truncatum          | 
|      31 | Fusarium oxysporium f. sp. ciceri | 
|      40 | Homalodisca vitripennis           | 
|      41 | Hordevirus BSMV                   | 
|      50 | Mayetiola hordei                  | 
|      51 | Meromyza saltatrix                | 
|      60 | Phyllophaga                       | 
|      61 | Pyrenophora teres                 | 
+--------+------------------------------------+

However -- I can't for the life of me work out how to include the last row of the subquery in the result set. 但是,我无法终生确定如何在结果集中包含子查询的最后一行。 Ie, the row with rownum 67 (or whatever) that does not meet the criteria of the WHERE clause. 即,具有不符合WHERE子句条件的rownum 67(或任何其他字符)的行。

I was hoping to somehow pull the maximum value of rownum and add it to the WHERE clause, but I'm having no joy. 我希望以某种方式拉出rownum的最大值并将其添加到WHERE子句中,但我并不高兴。

Any ideas? 有任何想法吗?

Happy to try to rephrase if this isn't clear! 如果不清楚,很高兴尝试改写!

Edit -- here's a more appropriate version of the subquery: 编辑-这是子查询的更合适的版本:

SELECT * FROM 
  ( 
  SELECT @row := @row + 1 AS `rownum`, `sort_field` FROM 
    ( 
    SELECT @row := 0 ) r, 
      (
      SELECT D.`id`, D.`display_name` as display_field,
      D.`sort_name` as sort_field 
      FROM Document D INNER JOIN 
        ( 
        SELECT DS.* FROM Document_Status DS INNER JOIN 
          ( 
          SELECT `document_id`, max(`datetime`) as `MaxDateTime` 
          FROM Document_Status GROUP BY `document_id` 
          ) 
        GS ON DS.`document_id` = GS.`document_id` 
        AND DS.`datetime` = GS.`MaxDateTime` 
        AND DS.`status` = 'approved' INNER JOIN 
          (
          SELECT `id` FROM Document WHERE `template_id`= 2 ) GD 
          ON DS.`document_id` = GD.`id` 
          ) 
        AG ON D.id = AG.document_id ORDER BY `sort_field` ASC 
        ) Subquery 
      ) Sorted 
      WHERE rownum % $num_rows_per_page = 1 OR rownum % $num_rows_per_page = 0

But, a key point to remember is that the subquery will change depending on the context. 但是,要记住的关键一点是子查询将根据上下文而变化。

Please try adding 请尝试添加

OR rownum=@row

to your WHERE clause (in my testing case this works) 到您的WHERE子句(在我的测试案例中,此方法有效)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM