在 MYSQL 中获取 MaxRow Count / 通过单次调用从过程中获取 FOUND_ROWS 和数据表

发布于 2024-10-19 19:25:59 字数 2147 浏览 1 评论 0原文

由于需要在 (ASP.NET) 网站的网格视图中显示记录。同样,我正在使用过程将数据返回到 .net Net Framework。

因为我的问题是我需要设置表并返回当前行(数据)的编号。页。我还需要分页的总行数(显示页面数)。

同样,我写了以下是我返回的过程

    CREATE PROCEDURE usp_SearchEmp(IN _RowIndex int, IN _MaxRows int , IN _SortByCol  varchar(40),IN _EmployeeID int)
 DECLARE _getLastSequenceNumberSQL VARCHAR(4000);
set _getLastSequenceNumberSQL ='
    SELECT SQL_CALC_FOUND_ROWS 
    @rownum:=@rownum+1 ROW,
        E.EmployeeID,
        E.EmployeeName  
    from
    (SELECT @rownum:=0) r, Employee E ;
        IF (_EmployeeID IS NOT NULL) THEN
              set _getLastSequenceNumberSQL = concat(_getLastSequenceNumberSQL,' WHERE (E.EmployeeID=',_EmployeeID,')');
        END IF;

        IF (_SortByCol IS NOT NULL) AND (_SortByCol != "") THEN
              set @SortCol = concat(' ORDER BY ', _SortByCol) ;
        ELSE
               set @SortCol = 'ORDER BY EmployeeName';
        END IF;

      set _getLastSequenceNumberSQL = concat(_getLastSequenceNumberSQL, @SortCol , ' LIMIT ',  _RowIndex, ' , ' , _MaxRows,' ');

 SET @getLastSequenceNumberSQL = _getLastSequenceNumberSQL;

  prepare lastRecordStmt from @getLastSequenceNumberSQL;
  execute lastRecordStmt;
  deallocate prepare lastRecordStmt;

    select FOUND_ROWS() as i;

,但现在的问题是,当我从 .net 调用这个过程时,它只返回 Found_Rows 的结果..

 I need out put . some thing like the following 

就像:-

SELECT 64 AS `ROWMAX`, E.EmployeeID, E.EmployeeName FROM Employee E order by EmployeeID LIMIT 0 , 4

|--------------------------------------------------------------|

| ROWMAX  | EmployeeID | EmployeeName                          |

|--------------------------------------------------------------|

|  64     |    1       | Emp One                               |

|  64     |    2       | Emp Two                               |

|  64     |    3       | Emp Three                             |

|  64     |    4       | Emp Four                              |

----------------------------------------------------------------

因为我可以使用 ROWMAX 列来网格视图中合理的页数。

所以请指导我同样的事情。因为我是 MYSQL 新手

As need to display Records in grid view in (ASP.NET) web site. and for the same i am using Procedure to return Data to .net Net Framework.

As my problem is that i need table set with return me the no of ROW's (Data) for the curr. Page. and i also need total no of row for paging (To display no of pages).

And for the same i have writen the following is the procedure which i have return

    CREATE PROCEDURE usp_SearchEmp(IN _RowIndex int, IN _MaxRows int , IN _SortByCol  varchar(40),IN _EmployeeID int)
 DECLARE _getLastSequenceNumberSQL VARCHAR(4000);
set _getLastSequenceNumberSQL ='
    SELECT SQL_CALC_FOUND_ROWS 
    @rownum:=@rownum+1 ROW,
        E.EmployeeID,
        E.EmployeeName  
    from
    (SELECT @rownum:=0) r, Employee E ;
        IF (_EmployeeID IS NOT NULL) THEN
              set _getLastSequenceNumberSQL = concat(_getLastSequenceNumberSQL,' WHERE (E.EmployeeID=',_EmployeeID,')');
        END IF;

        IF (_SortByCol IS NOT NULL) AND (_SortByCol != "") THEN
              set @SortCol = concat(' ORDER BY ', _SortByCol) ;
        ELSE
               set @SortCol = 'ORDER BY EmployeeName';
        END IF;

      set _getLastSequenceNumberSQL = concat(_getLastSequenceNumberSQL, @SortCol , ' LIMIT ',  _RowIndex, ' , ' , _MaxRows,' ');

 SET @getLastSequenceNumberSQL = _getLastSequenceNumberSQL;

  prepare lastRecordStmt from @getLastSequenceNumberSQL;
  execute lastRecordStmt;
  deallocate prepare lastRecordStmt;

    select FOUND_ROWS() as i;

But now the problem is that when i call this Procedure from .net it return me only the result of Found_Rows ..

 I need out put . some thing like the following 

like :-

SELECT 64 AS `ROWMAX`, E.EmployeeID, E.EmployeeName FROM Employee E order by EmployeeID LIMIT 0 , 4

|--------------------------------------------------------------|

| ROWMAX  | EmployeeID | EmployeeName                          |

|--------------------------------------------------------------|

|  64     |    1       | Emp One                               |

|  64     |    2       | Emp Two                               |

|  64     |    3       | Emp Three                             |

|  64     |    4       | Emp Four                              |

----------------------------------------------------------------

as i can user ROWMAX column to justified No. of Pages in grid view.

So please guide me out for the same. as i am new to MYSQL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人哭 2024-10-26 19:25:59

因为你可以制作动态程序。这只会计算所有的 MaxNo of Row。并将返回相同的值。并且可以从不同的过程中调用相同的内容。

As you can make dynamic Procedure . which will just count all the MaxNo of Row. and will return the same. and can call the same from different Procedure.'s

走野 2024-10-26 19:25:59

如果您可以使用多重查询,您可以在同一过程中拥有 2 个结果集(即在查询后添加 SELECT FOUND_ROWS(); 并使用 LIMIT 仅显示一页结果)

这里有一篇关于在 PHP 中实现它的好文章:
http://www.robert- gonzalez.com/2007/06/01/mysql-multiple-result-procs-in-php/

If you can use multi query you can have 2 result sets in the same procedure (i.e. add a SELECT FOUND_ROWS(); after your query and use LIMIT to display only one page results)

Here is a good post about implementing it in PHP:
http://www.robert-gonzalez.com/2007/06/01/mysql-multiple-result-procs-in-php/

筱武穆 2024-10-26 19:25:59

我们可以使用其他 PROCEDURE ,它将从 MainProcedure 中调用(比如说主程序)。

让我们说ProcedureCound它将包含Pr构建sql查询“SELECT COUNT(*)”
并且将接受两个参数(argument)

1)将是out参数
2) From条件是动态生成的。在主程序中。

所以现在需要附加第二个参数,其中包含我们从主程序获得的条件。
并将结果存储到 out 参数中,我们可以将其返回到主程序中。

we can use other PROCEDURE Which will be call from MainProcedure(let say Main Procedure).

let says ProcedureCound it will contain Pr build sql query " SELECT COUNT(*) "
and will accept two parameter (argument)

1) will be out parameter
2) will be From condition which is dynamic generated. in Main Procedure.

so now need to append second argument which contains from condition.which we got from Main Procedure..
and stored the result in to out parameter which we can access back into Main Procedure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文