Oracle processes parent-child node relationships, and oracle Parent-Child Nodes

Source: Internet
Author: User
Tags dname

Oracle processes parent-child node relationships, and oracle Parent-Child Nodes

It is very complicated to process the relationship between tree structures. We may traverse parent nodes or child nodes layer by layer through program code. The disadvantages of doing so are obvious, low efficiency and high operation complexity. When using the Oracle database, we can have a simple solution, as shown below:

 

1. Create a table to save the parent-child relationship.

Drop table tmp_node;

Create table tmp_node (id varchar2 (500), p_id varchar2 (500 ));

 

2. Insert data into the table

Insert into tmp_node (p_id, id) values (null, 1 );

Insert into tmp_node (p_id, id) values (1, '1-1 ');

Insert into tmp_node (p_id, id) values (1, '1-2 ');

Insert into tmp_node (p_id, id) values ('1-1', '1-1-1 ');

Insert into tmp_node (p_id, id) values ('1-1', '1-1-2 ');

Insert into tmp_node (p_id, id) values ('1-2', '1-2-1 ');

 

3. View table data

Select * from tmp_node;

No.

P_ID

ID

1

 

1

2

1

1-1

3

1

1-2

4

1-1

1-1-1

5

1-1

1-1-2

6

1-2

1-2-1

 

4. Create a view (this step is the most important)

Create or replace view tmp_node_view

Select distinct level lvl, connect_by_root (id) leaf_id, t .*

From tmp_node t

Connect by prior t. p_id = t. id;

 

5. view Data

Select * form tmp_node_view order by leaf_id, lvl;

No.

LVL

LEAF_ID

ID

P_ID

1

1

1

1

 

2

1

1-1

1-1

1

3

2

1-1

1

 

4

1

1-1-1

1-1-1

1-1

5

2

1-1-1

1-1

1

6

3

1-1-1

1

 

7

1

1-1-2

1-1-2

1-1

8

2

1-1-2

1-1

1

9

3

1-1-2

1

 

10

1

1-2

1-2

1

11

2

1-2

1

 

12

1

1-2-1

1-2-1

1-2

13

2

1-2-1

1-2

1

14

3

1-2-1

1

 

 

6. Instructions for use

The leaf_id and id fields are used.

Field Description: The lvl field indicates the depth of the node, and leaf_id indicates the leaf node. P_id: parent node id, id: id of the current node.

When you need to know the parent nodes of a node, you only need to query the id field based on leaf_id:

Select * from tmp_node_view where leaf_id = '1-2-1 ';

No.

LVL

LEAF_ID

ID

P_ID

1

1

1-2-1

1-2-1

1-2

2

3

1-2-1

1

 

3

2

1-2-1

1-2

1

When you need to know which sub-nodes A node has, you only need to query leaf_id by id:

Select * from tmp_node_view where id = '1-2 ';

No.

LVL

LEAF_ID

ID

P_ID

1

1

1-2

1-2

1

2

2

1-2-1

1-2

1

Note: All queries on Parent and Child Nodes contain their own nodes.


How does jQuery determine the relationship between two sections, such as "parent-child relationship", "sibling relationship", and "parent-child relationship"

This is the case. In jQuery, "parent-child relationship" and "sibling relationship" are based on html DOM nodes.

For example, in your example,
<Div class = "div" id = div1>
<Font class = "fount1"> I am 1! </Font>
<Div id = div2>
<Font class = "fount2"> I am 2! </Font>
</Div>
</Div>
Div1 is a subnode under the body of the html page, and div1 has fount1 and div2. fount1 and div2 are sibling nodes, and div2 has a fount2 node.
You need jQuery to find the parent node and child node. First, you need to figure out the relationship between the node in HTML, and then you can use the parent () and child () methods.

The following is an example
<% @ Page language = "java" import = "java. util. *" pageEncoding = "GB18030" %>

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> My JSP 'test. jsp 'starting page </title>
<Script type = "text/javascript" src = "../js/jquery/jquery-1.3.2.min.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
Alert ($ (". fount1"). parents (". div" example .html ());
Alert ($ (". fount2"). parents (". div" example .html ());
});
</Script>

</Head>

<Body>
<Div class = "div">
<Font class = "fount1"> I am 1! </Font>
<Div>
<Font class = "fount2"> I am 2! </Font>
</Div>
</Div>
</Body>
</Html>... the remaining full text>

Oracle parent-child table Association, A, B parent-child table relationship, 1A => how does one merge one column of multiple data related to table B into Table A as A record?

Take the Department table and employee table under scott in oracle as an example.
SELECT * from emp;
SELECT * from dept;

Select d. DEPTNO, D. DNAME, D. LOC, WMSYS. WM_CONCAT (E. EMPNO)
From dept d, EMP E
Where d. DEPTNO = E. DEPTNO
Group by d. DEPTNO, D. DNAME, D. LOC;
You can merge employee numbers into one, where e. empno can be replaced with other fields.
Ask again if you have any questions.

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.