MySQL返回设置字段

时间:2015-02-19 09:06:23

标签: c# mysql sql jquery-datatables

您好我有一个MySQL查询,从c#调用并用于jQuery DataTables的服务器端分页

SELECT * FROM (SELECT (@rn := @rn + 1) AS RowNumber,item_id,description
FROM
    (SELECT (SELECT count(item.item_id)
            FROM
                item) AS TotalRows
        , ( SELECT  count(item_id) FROM item  ) AS TotalDisplayRows ,item_id,description
    FROM
        item  ) RawResults
    ) Results 
    WHERE RowNumber BETWEEN 1 AND 10

返回RowNumber,item_id,description

我还需要返回TotalRows和TotalDisplayRows。无法弄清楚如何做到这一点。请帮忙。感谢

这是在SQL中运行的查询

SELECT * FROM
(SELECT row_number() OVER (ORDER BY  NO  asc) AS RowNumber , *
    FROM
        (SELECT (SELECT count(item.item_id)
                FROM
                    item) AS TotalRows 
            , ( SELECT  count(item_id) FROM iq_item  ) AS TotalDisplayRows ,*
        FROM
            item ) RawResults) Results WHERE RowNumber BETWEEN 1 AND 10

2 个答案:

答案 0 :(得分:0)

解密您的查询非常复杂......

这是你想要的吗?

// returns first 10 results
SELECT SQL_CALC_FOUND_ROWS item_id, description FROM item LIMIT 0, 10;
// get total number of elements
SELECT FOUND_ROWS() AS TotalRows;

您的行号必须使用您的分页索引和返回的行位置计算,而TotalDisplayRows是返回的行数,如果我理解您打算做什么

答案 1 :(得分:0)

感谢@markpsmith我使用单独的查询对其进行排序,以获得总行数和总显示行数。

总行数

SELECT count(item.item_id) FROM item @where

总展示次数

SELECT count(item.item_id) FROM item @filteredwhere

主要查询

SET @rn := 0;
SELECT * FROM (SELECT (@rn := @rn + 1) AS RowNumber,item_id,description
FROM
    (SELECT (SELECT count(item.item_id)
            FROM
                item) AS TotalRows
        , ( SELECT  count(item_id) FROM item @where) AS TotalDisplayRows ,item_id,description
    FROM
        item where item_id @filteredwhere  ) RawResults
    ) Results 
    WHERE RowNumber BETWEEN 1 AND 50;