我正在使用带有 CSqlDataProvider 的分页。结果正确显示在分页的前 20 页或第一页。当我在网页上看到日志消息时,它使用的查询实际上是为所有页面选择前 20 行,这就是问题所在。我不知道如何纠正它。
我的控制器有代码:
public function actionLink()
{
$sql = "SELECT Ordernumber, Order_Date
FROM [Orders]
WHERE CAST(Order_Date As Date) BETWEEN '01-01-2014' AND '31-01-2014'";
$count = Yii::app()->db->createCommand('SELECT COUNT(*) FROM (' . $sql . ') as count_alias')->queryScalar();
$dataProvider = new CSqlDataProvider($sql, array('keyField' => 'Order_Date',
'totalItemCount' => $count,
'pagination' => array(
'pageSize' => 20,),));
$this->render('link', array('dataProvider' => $dataProvider));
}}
现在它用于计数的查询是正确的(日志消息):
查询 SQL:
SELECT COUNT(*) FROM (SELECT Ordernumber ,Order_Date
FROM [Orders]
WHERE CAST(Order_Date As Date) BETWEEN '01-01-2014' AND '31-01-2014') as count_alias
它用于第 2 页的查询是(日志消息):
SELECT * FROM (SELECT TOP 20 * FROM (SELECT TOP 40 Ordernumber,Order_Date
FROM [Orders]
WHERE CAST(Order_Date As Date)
BETWEEN '01-01-2014' AND '31-01-2014') as [__inner__]) as [__outer__]
上面的查询实际上会给出前 20 行的结果。每一页都有同样的问题。它将每个结果作为相同的前 20 行。所以第3页变成(日志消息):
SELECT * FROM (SELECT TOP 20 * FROM (SELECT TOP 60 Ordernumber,Order_Date
FROM [Orders]
WHERE CAST(Order_Date As Date)
BETWEEN '01-01-2014' AND '31-01-2014')
as [__inner__] ) as [__outer__]
我能看到的唯一问题是它用于检索第 2,3 页的数据的查询......等等。对于每一页,它都会返回相同的数据,因为每次它都选择前 20 行。我不明白这种行为的原因。当您在分页期间SQL SERVER
用作数据库和数据提供者时,查询的实际外观如何?CSqlDataProvider