Sqlite3遞歸查詢,分頁查詢

版權聲明

本文首發自CSDN博客:http://blog.csdn.net/sisyphus_z

作者:alex_zuo

無需授權即可轉載,但請自覺保留以上版權聲明。


技術點:使用Sqlite3實現遞歸查詢,分頁查詢


向下遞歸

向上遞歸

Sqlite的分頁查詢
limit N表示取N個記錄.
offset P表示從下標P開始, 第一行的下標是0, 也可以理解爲跳過P行.
limit要放在查詢語句的最後:
select * from finery where id > 7 order by id limit 10 offset 0;
遞歸查詢可複製sql:
WITH RECURSIVE  down(id,name,pid,layer,sort,type_id,is_piece,piece_Id,type_tree_id,group_id) AS   
( 
    SELECT id,name,pid,layer,sort,type_id,is_piece,piece_Id,type_tree_id,group_id 
    FROM t_product_tree   
    WHERE id='2014112411014156812'   
    UNION   
    SELECT a.id,a.name,a.pid,a.layer,a.sort,a.type_id,a.is_piece,a.piece_Id,a.type_tree_id,a.group_id                                                                         
    FROM t_product_tree a,down b 
    WHERE b.id = a.pid   
) SELECT * from down 


WITH RECURSIVE  up(id,name,pid,layer,sort,type_id,is_piece,piece_Id,type_tree_id,group_id) AS   
( 
    SELECT id,name,pid,layer,sort,type_id,is_piece,piece_Id,type_tree_id,group_id 
    FROM t_product_tree   
    WHERE id='2014112411014156812'   
    UNION   
    SELECT a.id,a.name,a.pid,a.layer,a.sort,a.type_id,a.is_piece,a.piece_Id,a.type_tree_id,a.group_id                                                                         
    FROM t_product_tree a,up b 
    WHERE b.pid = a.id   
) SELECT * from up 



發佈了73 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章