Tree query of common Oracle databases

Source: Internet
Author: User

The recursion data in the tree query table of common Oracle databases is as follows: id name parentid1 food classification-12 Meat 13 vegetables 14 product classification-15 health care products 46 Medicine 47 Building 4 a oracle implementation method: directly supported in Oracle, use the statement select * from tablename start with connect byprior id (child-layer column) = parentid (top-level column: 13061124399 start with specifies the conditions for starting a hierarchy. That is, the rows that meet this condition can be used as the association conditions between the top-level connect by prior layers of the hierarchy tree, that is, the query result of select * from recursionstart with connect by prior> is as follows: id name parentid1 food classification-12 Meat 13 vegetables Class 1 ii mssql Implementation Method in MSSQL requires the use of temporary tables and loop multiple queries. create function: create function GetRecursion (@ id int) returns @ t table (idint, namevarchar (50), parentidint) asbegin insert @ tselect * from recursion where> while @ rowcount> 0 insert @ t select. * from recursion as a inner join @ t as B on. parentid = B. id and. id not in (select id from @ t) returnend usage method: select * from GetRecursion (4) query result: id name parentid4 product category-15 health care products 46 Medicine 47 Building 4 3 MYSQL implementation method query statement: select B. id, B. name, B. parentid from recursion as a, recursion as bwherea. id = B. parentid and (. id = 1 or. parentid = 1) query results: id name parentid2 Meat 13 vegetables Class 1 4 in ORACLE, MSSQL, MYSQL, you can use the following query statement to return only the child node data of the tree structure table select * from tablename t where not exists (select' x' from tablename t1, tablename t2 where t1.id = t2.parentid and t1.id = t. id) for example: select * from recursion t where not exists (select 'x' from recursion t1, recursion t2 where t1.id = t2.parentid and t1.id = t. id) query result: id name parentid2 Meat 13 Vegetables 15 health care products 46 Medicine 47 Building 4 5 in ORACLE, MSSQL, MYSQL you can use the following query statement to return only the root node data of the tree table select * from tablename t where not exists (select 'x' from tablename t1, tablename t2 where t1.id = t2.parentid and t1.id = t. parentid) for example: select * from recursion t where not exists (select 'x' from recursion t1, recursion t2 where t1.id = t2.parentid and t1.id = t. parentid) Query Result: id name parentid1 food category-14 product category-1

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.