admin 管理员组

文章数量: 1086019


2024年4月14日发(作者:安装并配置oracle11g步骤)

mysql 递归语句写法

英文回答:

MySQL does not support recursive queries natively like

some other databases do. However, there are ways to achieve

recursion in MySQL by using temporary tables and stored

procedures. One common approach is to use a recursive

stored procedure that repeatedly inserts records into a

temporary table until a termination condition is met.

To illustrate this, let's consider an example where we

have a table called "employees" with columns "id" and

"manager_id". The "manager_id" column represents the ID of

the employee's manager. We want to find all the employees

who report directly or indirectly to a given manager.

First, we create a temporary table to store the results:

CREATE TEMPORARY TABLE temp_employees (id INT);

Next, we define a stored procedure that inserts the

initial employee into the temporary table and recursively

inserts the employees who report to the inserted employee:

DELIMITER //。

CREATE PROCEDURE find_subordinates(IN manager_id INT)。

BEGIN.

-Insert the initial employee into the temporary

table.

INSERT INTO temp_employees VALUES (manager_id);

-Recursively insert the employees who report to the

inserted employee.

INSERT INTO temp_employees.

SELECT id FROM employees WHERE manager_id =

manager_id;


本文标签: 配置 安装 递归 语句 写法