search
HomeDatabaseOracleOne article analyzing ORACLE tree structure query

This article brings you relevant knowledge about Oracle. It mainly introduces the article about parsing ORACLE tree structure query. The article expands on the topic in detail. Let’s take a look at it together. I hope Helpful to everyone.

One article analyzing ORACLE tree structure query

Recommended tutorial: "Oracle Video Tutorial"

In our daily programming, we often encounter tree structures Represents, for example, organizational structures, administrative divisions, etc. These are often displayed through a table in the database. Here we take a simple administrative division table as an example. In actual use, other description fields and levels can be added to it.

#The table is associated with ID and PID to achieve tree structure storage. The table creation and data statements are as follows:

-- Create table
create table TREETEST
(
  id   NVARCHAR2(50),
  pid  NVARCHAR2(50),
  name NVARCHAR2(50)
)
insert into TREETEST (ID, PID, NAME) values ('1', null, '山东省');
insert into TREETEST (ID, PID, NAME) values ('2', '1', '青岛市');
insert into TREETEST (ID, PID, NAME) values ('3', '1', '烟台市');
insert into TREETEST (ID, PID, NAME) values ('4', null, '河南省');
insert into TREETEST (ID, PID, NAME) values ('5', null, '河北省');
insert into TREETEST (ID, PID, NAME) values ('6', '2', '市南区');
insert into TREETEST (ID, PID, NAME) values ('7', '2', '市北区');
insert into TREETEST (ID, PID, NAME) values ('8', '2', '即墨市');

How to query the tree structure? Oracle provides recursive query for query. The basic syntax is as follows:

SELECT [Column]…..
  FEOM [Table]
  WHERE Conditional1
  START WITH Conditional2
  CONNECT BY PRIOR Conditional3
  ORDER BY [Column]

Description:

  • Condition 1---Filter conditions, for all Returned records are filtered.
  • Condition 2---The limiting condition of the root node. Of course, you can also relax the permissions to obtain multiple root nodes, that is, obtain multiple trees
  • Condition 3---The link condition, the purpose is Give what is the relationship between father and son, and perform a recursive query based on this relationship (in the above table, ID=PID)
  • Sort---Sort all returned records

Let’s look at specific examples below:

1. Query all sub-nodes under Shandong Province

SELECT *
  FROM TREETEST t
 START WITH t.PID=1
CONNECT BY PRIOR t.ID = t.PID

where the ID is 1 is the node of Shandong Province, and the query results are as follows:

#2. Query the next-level child node of Qingdao City (note the difference from the above, all Child nodes and next-level child nodes)

One article analyzing ORACLE tree structure query

3. If you need to connect Qingdao City, Shandong Province, etc. to display, you can use SYS_CONNECT_BY_PATH to achieve this

SELECT t.ID, SYS_CONNECT_BY_PATH(t.NAME, '\') AS INDU_NAME
  FROM TREETEST t
 START WITH t.PID IS NULL
CONNECT BY PRIOR t.ID = t.PID

The query results are as follows:

4. In the same way, you can also proceed from bottom to top. Query

SELECT *
  FROM TREETEST t
 START WITH t.ID=8
CONNECT BY t.ID = PRIOR t.PID

Recommended tutorial: "Oracle Video Tutorial"

The above is the detailed content of One article analyzing ORACLE tree structure query. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
Oracle's Impact: Data Management and BeyondOracle's Impact: Data Management and BeyondApr 27, 2025 am 12:11 AM

Oracle has a profound impact in the fields of data management and enterprise applications. Its database is known for its reliability, scalability and security, and is widely used in industries such as finance, medical care and government. Oracle's influence has also expanded to middleware and cloud computing fields such as WebLogicServer and OracleCloudInfrastructure (OCI), providing innovative solutions. Despite the competition in the open source database and cloud computing market, Oracle maintains its leading position through continuous innovation.

Oracle: Exploring the Company's Mission and ValueOracle: Exploring the Company's Mission and ValueApr 26, 2025 am 12:06 AM

Oracle's mission is to "help people see the value of data", and its core values ​​include: 1) Customer first, 2) Integrity, 3) Innovation, and 4) Teamwork. These values ​​guide Oracle's strategic decision-making and business innovation in the market.

Oracle's Core Function: Providing Database SolutionsOracle's Core Function: Providing Database SolutionsApr 25, 2025 am 12:06 AM

Oracle Database is a relational database management system that supports SQL and object relational models to provide data security and high availability. 1. The core functions of Oracle database include data storage, retrieval, security and backup and recovery. 2. Its working principle involves multi-layer storage structure, MVCC mechanism and optimizer. 3. Basic usages include creating tables, inserting and querying data; advanced usages involve stored procedures and triggers. 4. Performance optimization strategies include the use of indexes, optimized SQL statements and memory management.

Using Oracle Software: Database Management and BeyondUsing Oracle Software: Database Management and BeyondApr 24, 2025 am 12:18 AM

In addition to database management, Oracle software is also used in JavaEE applications, data grids and high-performance computing. 1. OracleWebLogicServer is used to deploy and manage JavaEE applications. 2. OracleCoherence provides high-performance data storage and caching services. 3. OracleExadata is used for high performance computing. These tools allow Oracle to play a more diversified role in the enterprise IT architecture.

Oracle's Role in the Business WorldOracle's Role in the Business WorldApr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Oracle Software in Action: Real-World ExamplesOracle Software in Action: Real-World ExamplesApr 22, 2025 am 12:12 AM

Oracle software applications in the real world include e-commerce platforms and manufacturing. 1) On e-commerce platforms, OracleDatabase is used to store and query user information. 2) In manufacturing, OracleE-BusinessSuite is used to optimize inventory and production planning.

Oracle Software: Applications and IndustriesOracle Software: Applications and IndustriesApr 21, 2025 am 12:01 AM

The reason why Oracle software shines in multiple fields is its powerful application and customized solutions. 1) Oracle provides comprehensive solutions from database management to ERP, CRM, SCM, 2) its solutions can be customized according to industry characteristics such as finance, medical care, manufacturing, etc. 3) Successful cases include Citibank, Mayo Clinic and Toyota, 4) The advantages lie in comprehensiveness, customization and scalability, but challenges include complexity, cost and integration issues.

Choosing Between MySQL and Oracle: A Decision GuideChoosing Between MySQL and Oracle: A Decision GuideApr 20, 2025 am 12:02 AM

Choosing MySQL or Oracle depends on project requirements: 1. MySQL is suitable for small and medium-sized applications and Internet projects because of its open source, free and ease of use; 2. Oracle is suitable for core business systems of large enterprises because of its powerful, stable and advanced functions, but at a high cost.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools