1.兩種方式 1.1:row_number() over()函數 1.2:OFFSET ....FETCH OFFSET { integer_constant | offset_row_count_expression } { ROW | ROWS } 指定在從查詢表達式中開始返回行之前,將跳過的行 ...
1.兩種方式
1.1:row_number() over()函數
1.2:OFFSET ....FETCH
OFFSET { integer_constant | offset_row_count_expression } { ROW | ROWS }
指定在從查詢表達式中開始返回行之前,將跳過的行數。OFFSET 子句的參數可以是大於或等於零的整數或表達式。ROW 和 ROWS 可以互換使用。
FETCH { FIRST|NEXT } <行計數表達式> { ROW|ROWS } ONLY
指定在處理 OFFSET 子句後,將返回的行數。FETCH 子句的參數可以是大於或等於 1 的整數或表達式。ROW 和 ROWS 可以互換使用。同樣,FIRST 和 NEXT 可以互換使用。
每頁10條,查詢第10頁的數據
select * from (select row_number() over(order by U_sortNo) as Rm,* from A01 where u_areacode='130000') as t where 100<Rm and Rm<=110
SELECT A0100,COUNT(1) OVER() AS total
FROM A01
where u_areacode='130000'
ORDER BY U_sortNo
OFFSET 100 ROWS FETCH NEXT 10 ROWS ONLY
說明跳過100行不包括第100條數據,並查出來之後的10的數據,也就是101-110中的十條數據
/// 分頁 /// </summary> /// <param name="pageIndex">獲取當前頁碼預設為1</param> /// <param name="pageSize">獲取每頁顯示多少條記錄</param> public string GetUserList(int pageIndex, int pageSize,string orderField, string orderType) { int count; string sql = @"select a.*,b.Title,b.Content from Power_user a left join Knowledge b on a.ID = b.CreateUserID"; string strsql = string.Format("select * from ({0}) T_TT where 1=1 ", sql); StringBuilder stringBuilder = new StringBuilder(); int num = (pageIndex-1) * pageSize; int num2 = pageIndex * pageSize; string str; if (!string.IsNullOrEmpty(orderField)) { str = "Order By " + orderField + " " + orderType; } else { str = "order by (select 0)"; } stringBuilder.Append("Select * From (Select ROW_NUMBER() Over (" + str + ")"); stringBuilder.Append(string.Concat(new object[] { " As rowNum, * From (", strsql, ") As T ) As N Where rowNum > ", num, " And rowNum <= ", num2 })); count = System.Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, CommandType.Text, "Select Count(1) From (" + strsql + ") As t", null)); ; DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionString, CommandType.Text, stringBuilder.ToString(), null); string Json = JsonConvert.SerializeObject(dt); List<Power_User> userModel = SqlHelper.ExecuteClass<Power_User>(SqlHelper.ConnectionString, strsql, null); //string b = JsonConvert.SerializeObject(userModel); return Json; }