oracle先排序再分頁

2021-09-12 16:13:10 字數 3079 閱讀 6086

oracle排序分頁查詢和mysql資料庫的語句還不一樣,這裡做簡單的記錄。
select a.*, rownum rn from (select * from v_log) a order by operatetime desc
結果

可以發現,按時間排序了,但是rownum並不是從小到大,因為oracle是先生成rownum,再進行排序,需要在套一層查詢

select t.*, rownum rn from(

select * from (select * from v_log) order by operatetime desc

) t

結果:

順序正確,rownum正確,在此基礎上再套一層查詢進行分頁

## 子查詢先找出所有,然後再rownum,rownum 為偽列,後再排序

select a."sku", a."goods_sn", rownum rn from (select * from amz_huopin_sku) a order by a."sku" desc

## 因為oracle是先生成rownum,再進行排序,需要在套一層查詢,即先拍好序,然後再生成rownum

select t."sku", t."goods_sn", rownum rn from (select * from (select * from amz_huopin_sku) order by "sku" desc

) t## 上邊的這兩個語句是等價的

select t."sku", t."goods_sn", rownum rn from (select * from amz_huopin_sku order by "sku" desc

) tselect t2.* from(

select t."sku", t."goods_sn", rownum rn from (select * from amz_huopin_sku order by "sku" desc

) t) t2 where rn between 0 and 10

# 1、子句查詢,這條語句可以加條件where

select * from (select a."sku", a."goods_sn", a."category",a."color_name", a."size_name", a."fnsku",h.qty,h.warehouseid,h.f_create_time from amz_huopin_sku a

left join hwc_camel_inv_all h on a."fnsku" = h.sku)

# 2、排序

select * from (select * from (select a."sku", a."goods_sn", a."category",a."color_name", a."size_name", a."fnsku",h.qty,h.warehouseid,h.f_create_time from amz_huopin_sku a

left join hwc_camel_inv_all h on a."fnsku" = h.sku)) order by "goods_sn" desc

# 3、排序之後,再獲取rownum

select t.*, rownum rn from (select * from (select * from (select a."sku", a."goods_sn", a."category",a."color_name", a."size_name", a."fnsku",h.qty,h.warehouseid,h.f_create_time from amz_huopin_sku a

left join hwc_camel_inv_all h on a."fnsku" = h.sku)) order by "goods_sn" desc) t

# 4、再根據獲取到的rownum按照順序進行分頁

select r.* from(

select t.*, rownum rn from (select * from (select * from (select a."sku", a."goods_sn", a."category",a."color_name", a."size_name", a."fnsku",h.qty,h.warehouseid,h.f_create_time from amz_huopin_sku a

left join hwc_camel_inv_all h on a."fnsku" = h.sku where a."goods_sn" = 'k832026565')) order by "goods_sn" desc) t

) r where rn between 10 and 20

oracle先排序再分頁

oracle排序分頁查詢和mysql資料庫的語句還不一樣,這裡做簡單的記錄。select a.rownum rn from select from v log a order by operatetime desc結果 可以發現,按時間排序了,但是rownum並不是從小到大,因為oracle是先生成...

oracle先排序再分頁

oracle排序分頁查詢和mysql資料庫的語句還不一樣,這裡做簡單的記錄。select a.rownum rn from select from v log a order by operatetime desc結果 可以發現,按時間排序了,但是rownum並不是從小到大,因為oracle是先生成...

mysql 先排序再分組的sql語句實現

最近專案中有乙個需求,需要先分組,再排序的功能。搞了好久,經過敏大大 後台兄弟 指導,終於搞出來了,分享給大家 demo 學生資訊表 第一時間想到 sql select from t test group by name,type order by score desc 結果 發現,並不能滿足我們需...