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;
        -Repeat the process for each newly inserted employee.
        WHILE ROW_COUNT() > 0 DO.
            INSERT INTO temp_employees.
            SELECT id FROM employees WHERE manager_id IN (SELECT id FROM temp_employees);
        END WHILE;
        -Return the final result.
        SELECT  FROM temp_employees;
    END //。
    DELIMITER ;
    Now, we can call the stored procedure with the ID of the manager we want to find the subordinates for:php调用mysql数据库

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。