gpt4 book ai didi

php - 有没有更好的方法来执行类似于 SQL Server 2008 中的 LIMIT 的操作?

转载 作者:行者123 更新时间:2023-11-29 14:32:42 25 4
gpt4 key购买 nike

我一直在努力寻找适合我的 Web 应用程序的分页脚本的最佳解决方案。我已经进行了充分的研究,知道在 SQL Server 中使用 rownum 可以替代 LIMIT,但我无法按照自己的方式得到我想要的结果。这是我的旧代码,如果一个表包含数百个产品,这将是一个非常糟糕的方法,我所做的就是更改 rownums 中的查询范围:

$page = $_GET['pageinfo'];

if (!(isset($page)))

{
$page=1;
}


if($page==1)
{

$query1 = " SELECT *
FROM (SELECT ROW_NUMBER() OVER(ORDER BY range) AS
rownum, productID, productName, category, range, price, picture, description FROM products) AS products1
WHERE rownum >= 0 AND rownum <= 9";


$result1 = sqlsrv_query( $conn, $query1, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));



if( $result1 === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}


while( $row = sqlsrv_fetch_array ( $result1, SQLSRV_FETCH_ASSOC))
{
echo '<p class="productsContent">'.$row['productName'];
echo '<br/>'.$row['description'];
echo '<br/>'.$row['category'];
echo '<br/>'.$row['range'];
echo '<br/>&pound;'.$row['price'];
echo '<br/><br/><a href="products.php?productID='.$row['productID'].'"><img src="' . $row['picture'] .'" alt="' . $row['productName'] .'" title="' . $row['productName'] .'" /></a>';

echo '<br/><br/></p>';
}

echo'<a href="products.php?pageinfo=2&amp;main=go">Page two</a>';
}

else if($page==2)
{

$query2 = " SELECT *
FROM (SELECT ROW_NUMBER() OVER(ORDER BY range) AS
rownum, productID, productName, category, range, price, picture, description FROM products) AS products1
WHERE rownum >= 10 AND rownum <= 20";


$result2 = sqlsrv_query( $conn, $query2, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));


if( $result2 === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}


while( $row = sqlsrv_fetch_array ( $result2, SQLSRV_FETCH_ASSOC))
{
echo '<p class="productsContent">'.$row['productName'];
echo '<br/>'.$row['description'];
echo '<br/>'.$row['category'];
echo '<br/>'.$row['range'];
echo '<br/>&pound;'.$row['price'];
echo '<br/><br/><a href="products.php?productID='.$row['productID'].'"><img src="' . $row['picture'] .'" alt="' . $row['productName'] .'" title="' . $row['productName'] .'" /></a>';
echo '<br/><br/></p>';

}

echo'<a href="products.php?pageinfo=1&amp;main=go">Page one</a>';

}

}

我遵循了一个教程,该教程似乎有更好的解决方案,但这是使用 MySQL 完成的,而我却陷入了使用 SQL Server 的困境。这是我的最新代码:

 if (!(isset($pagenum))) 

{

$pagenum = 1;

}

$page_rows = 9;


$stmt = sqlsrv_query( $conn, "select * from products $max" , array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

$rows = sqlsrv_num_rows( $stmt );

$last = ceil($rows/$page_rows);

if ($pagenum < 1)

{

$pagenum = 1;

}

elseif ($pagenum > $last)

{

$pagenum = $last;

}


$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;




while($info = sqlsrv_fetch_array( $stmt ))

{

Print $info['description'];

echo "<br>";

}

echo "<p>";


echo " --Page $pagenum of $last-- <p>";


echo " --Page $pagenum of $last-- <p>";


if ($pagenum == 1)

{

}

else

{

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";

echo " ";

$previous = $pagenum-1;

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";

}


if ($pagenum == $last)

{

}

else {

$next = $pagenum+1;

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";

echo " ";

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";

}

您可以看到它使用了“限制”,我花了一段时间才意识到 SQL Server 不支持它。

所以我的问题是混合我的两种方法,我只是不知道如何有效地做到这一点,任何建议将不胜感激。

最佳答案

这是一个非常通用的模板。

with rnkd as (select row_number() over (order by [yoursortfield]) rn, *
from [yourtable])
select *
from rnkd
where rn >= @start and rn < @end
order by rn

或者,您可以使用顶部运算符。 rn 上的 order by 在这里很重要:

with rnkd as (select row_number() over (order by [yoursortfield]) rn, *
from [yourtable])
select top (@pagesize) *
from rnkd
where rn >= @start
order by rn

或者,您可以避免 CTE 并按照派生表进行操作,但我个人更喜欢 CTE。

select top (@pagesize) *
from (select row_number() over (order by [yoursortfield]) rn, *
from [yourtable]) rnkd
where rn >= @start
order by rn

关于php - 有没有更好的方法来执行类似于 SQL Server 2008 中的 LIMIT 的操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9711712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com