Examples of implementations of SQLite using CTE and recursion

Source: Internet
Author: User
Tags parent directory sqlite



1. Multi-level CTE query example.


 
 
with cte as
(
   select pageid 
   from cm_bookpage
) , cte2 as
(
  select pageid, 2 as content from cte
)
select * from cte2


2. CTE recursive query article header level, 3872 a leaf node, to query out all the parent directory and sort back.


 
with cte as
(
  select pageid,bookid,title,parentpageid,1 as orderid
  from cm_bookpage 
  where pageid = 3872        
  union all  
  select a.pageid,a.bookid,a.title,a.parentpageid,(cte.orderid+1) as orderid
  from cm_bookpage a  
    inner join cte       
      on a.pageid = cte.parentpageid  
)
select *
from cte
order by orderid desc 





Examples of implementations of SQLite using CTE and recursion


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.