How to query the organizational structure (organizational structure tree)

Create a simple organizational sheet

  • Field 1: Organization ID
  • Field 2: Organization name
  • Field 3: Organization’s parent ID
-- 创建组织结构表
CREATE TABLE organization (
    id VARCHAR(36) PRIMARY KEY,
    name VARCHAR(100),
    parent_id VARCHAR(36)
);

Insert some data

INSERT INTO organization (id, name, parent_id)
VALUES
    ("00", '组织A', NULL),
    ("11", '组织B', NULL),
    ("0001", '子组织A1', "00"),
    ("0002", '子组织A2', "00"),
    ("1101", '子组织B1', "11"),
    ("1102", '子组织B2', "11"),
    ("000101", '子组织A1.1', "0001"),
    ("000102", '子组织A1.2', "0001"),
    ("000201", '子组织A2.1', "0002"),
    ("000202", '子组织A2.2', "0002"),
    ("110101", '子组织B1.1', "1101"),
    ("110102", '子组织B1.2', "1101"),
    ("110201", '子组织B2.1', "1102"),
    ("110202", '子组织B2.2', "1102");

Query organization

1. Query all third-level organizations

  • sql
SELECT 
    parent.id AS parent_id,
    parent.name AS parent_name,
    child1.id AS child1_id,
    child1.name AS child1_name,
    child2.id AS child2_id,
    child2.name AS child2_name
FROM 
    organization AS parent
JOIN 
    organization AS child1 ON parent.id = child1.parent_id
JOIN 
    organization AS child2 ON child1.id = child2.parent_id
WHERE 
    parent.parent_id IS NULL
  • Output results
    insert image description here

2. Query all organizations under organization A (check all organizations under the first level)

Because the IDs here are regular and can be identified by prefixes, use like fuzzy query. If the ID cannot be identified by prefixes, it cannot be used.

  • sql
SELECT 
    parent.id AS parent_id,
    parent.name AS parent_name,
    child1.id AS child1_id,
    child1.name AS child1_name,
    child2.id AS child2_id,
    child2.name AS child2_name
FROM 
    organization AS parent
JOIN 
    organization AS child1 ON parent.id = child1.parent_id
JOIN 
    organization AS child2 ON child1.id = child2.parent_id
WHERE 
    parent.parent_id IS NULL  and child2.id like '00%';
  • Output results
    insert image description here

3. Query all organizations under B1 organization (check all organizations under the second level)

Or use the characteristics of prefix for fuzzy query.

  • sql
SELECT 
    parent.id AS parent_id,
    parent.name AS parent_name,
    child1.id AS child1_id,
    child1.name AS child1_name,
    child2.id AS child2_id,
    child2.name AS child2_name
FROM 
    organization AS parent
JOIN 
    organization AS child1 ON parent.id = child1.parent_id
JOIN 
    organization AS child2 ON child1.id = child2.parent_id
WHERE 
    parent.parent_id IS NULL  and child2.id like '1101%';
  • Output resultsinsert image description here

4. Query B1.1 organization (specific third-level organization)

  • sql
SELECT 
    parent.id AS parent_id,
    parent.name AS parent_name,
    child1.id AS child1_id,
    child1.name AS child1_name,
    child2.id AS child2_id,
    child2.name AS child2_name
FROM 
    organization AS parent
JOIN 
    organization AS child1 ON parent.id = child1.parent_id
JOIN 
    organization AS child2 ON child1.id = child2.parent_id
WHERE 
    parent.parent_id IS NULL  and child2.id like '110101%';
  • Output results
    insert image description here

5. How to write mybatis

When using mybatis' xml to write sql, you only need to use #{ID} to replace parameters and splice them.

<where>
	<if test="id != null and id != ''">
		id like CONCAT(#{id}, '%')
	</if>
</where>

No prefix rule

1. Query all organizations under organization B

SELECT 
    parent.id AS parent_id,
    parent.name AS parent_name,
    child1.id AS child1_id,
    child1.name AS child1_name,
    child2.id AS child2_id,
    child2.name AS child2_name
FROM 
    organization AS parent
JOIN 
    organization AS child1 ON parent.id = child1.parent_id
JOIN 
    organization AS child2 ON child1.id = child2.parent_id
WHERE 
    parent.id = "11"; -- 要查询的组织B的id

2. Query all organizations under B1 organization

SELECT 
    parent.id AS parent_id,
    parent.name AS parent_name,
    child1.id AS child1_id,
    child1.name AS child1_name,
    child2.id AS child2_id,
    child2.name AS child2_name
FROM 
    organization AS parent
JOIN 
    organization AS child1 ON parent.id = child1.parent_id
JOIN 
    organization AS child2 ON child1.id = child2.parent_id
WHERE 
    child1.id = "1101"; -- 要查询的组织B1的id

3. Query the third-level B1.1 organization

SELECT 
    parent.id AS parent_id,
    parent.name AS parent_name,
    child1.id AS child1_id,
    child1.name AS child1_name,
    child2.id AS child2_id,
    child2.name AS child2_name
FROM 
    organization AS parent
JOIN 
    organization AS child1 ON parent.id = child1.parent_id
JOIN 
    organization AS child2 ON child1.id = child2.parent_id
WHERE 
    child2.id = "110101"; -- 要查询的组织B1.1的id

4. The sql writing method of mybatis’s xml

Here idOne is the first-level ID, idTwo is the second-level ID, and idThree is the third-level ID, and only one can be passed, so that all organizations below can be found when passing the first level.

<where>
	<if test="idOne != null and idOne != ''">
		parent.id = #{idOne}
	</if>
	<if test="idTwo != null and idTwo != ''">
		child1.id = #{idTwo}
	</if>
	<if test="idThree != null and idThree != ''">
		child2.id = #{idThree}
	</if>
</where>

This is the method I can think of at the moment. If everyone has a better solution, we can share it together.

Finish! ! ! ! !
hy:19


						人而不学,其犹正墙面而立。--《尚书》

Guess you like

Origin blog.csdn.net/weixin_49107940/article/details/131596243
Recommended