如果的單一字段排序分頁,現在有很多的存儲過程和SQL語句,分頁的時候,只取pageSize的記錄,可遇見的問題是:
這個單一字段必須是唯一的
這個字段必須是可以被排序的
不支持多字段排序
針對這一問題,我用C#做了一個類,解決以上的對多字段排序分頁和每次都取pageSize條記錄的問題 先看看代碼:
復制代碼 代碼如下:
using System;
using System.Collections.Specialized;
namespace web
{
/// <summary>
/// MultiOrderPagerSQL 的摘要說明
/// </summary>
public class MultiOrderPagerSQL
{
private NameValueCollection orders = new NameValueCollection();
private string table_;
private string where_="";//1=1 and 2=2 的格式
private string outfields_;
private int nowPageIndex_=0;
private int pagesize_=0;
private string sql_;//要返回的SQL
public MultiOrderPagerSQL()
{
}
/****************方法*******************/
public void addOrderField(string field, string direction)
{
orders.Add(field, direction);
}
public string getSQL()
{
//排序字段
string orderList="";//用戶期望的排序
string orderList2 = "";//對用戶期望的排序的反排序
string orderList3 = "";//用戶期望的排序,去掉了前綴.復合查詢裡的外層的排序不能是類似這樣的table1.id,要去掉table1.。
if (orders.Count > 0)
{
string[] str = orders.AllKeys;
foreach (string s in str)
{
string direction="asc";//默認一個方向
if (orders[s].ToString() == "asc")
direction = "desc";
//去掉前綴的字段名稱
string s2 = "";
int index = s.IndexOf(".") + 1;
s2 = s.Substring(index);
orderList =orderList + s +" "+ orders[s] +",";
orderList2 = orderList2 + s2 + " " + direction + ",";
orderList3 = orderList3 + s2 + " " + orders[s] + ",";
}
//去掉最後的,號
orderList = orderList.Substring(0,orderList.Length-1);
orderList2 = orderList2.Substring(0, orderList2.Length - 1);
orderList3 = orderList3.Substring(0, orderList3.Length - 1);
}
//return orderList2;
//形成SQL
string strTemp;
strTemp = "select * from \n ( select top {7} * from ( select top {6} {0} from {1} \n";
if (where_ != "")
strTemp = strTemp + " where {2} \n";
if(orderList!="")
strTemp = strTemp + " order by {3} ) as tmp order by {4} \n ) \n as tmp2 \n order by {5} \n";
strTemp = string.Format(strTemp, outfields_, table_, where_, orderList, orderList2, orderList3, nowPageIndex_ * pagesize_, pagesize_);
return strTemp;
}
/****************屬性*******************/
public string table
{
set { table_ = value; }
}
public string where
{
set { where_ = value; }
}
public string outfields
{
set { outfields_ = value; }
}
public int nowPageIndex
{
set { nowPageIndex_ = value; }
}
public int pagesize
{
set { pagesize_ = value; }
}
}
}
說一下原理先:其實很簡單,由於AC和MS SQL 2000 沒有象MS SQL 2005的row_number函數,我們就不能從這裡下手了,比如你取第二頁,那就是序號從10-20,我們先按照某一排序規則 把 前 20條的數據取出來,然後再按照先前的排序規則的反規則把這個數據反排序,再取前10條,那麼這個時候就是要取的數據了,這個時候還沒有結束,再把結果按照先前的排序規則排序即可。我覺得效率瓶頸會出現在排序上。看看是怎麼來使用的:
復制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MultiOrderPagerSQLTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
web.MultiOrderPagerSQL sql = new web.MultiOrderPagerSQL();
//sql.addOrderField("t1.id", "desc");//第一排序字段
sql.addOrderField("t1.hits", "desc");//第二排序字段
sql.table = "joke t1,type t2";
sql.outfields = "t1.*,t2.type";
sql.nowPageIndex = 5;
sql.pagesize = 10;
sql.where = "t1.typeid=t2.typeid";
Response.Write(sql.getSQL());
}
}
以上在AC和MS SQL 2000(5)上測試通過。
暫時做出這樣一個類,沒有做成存儲過程,要做的話,還有一點難度呢 ,呵呵。