Current location - Music Encyclopedia - Chinese History - Recursive call in mysql
Recursive call in mysql
First, create a familiar organization table.

Insert several test data:

Union all is an initialization statement, which will only be executed once. Find this line of records in the development department.

Next, the following join will use the initialized statement to go to the original organization table, get all the sub-departments of the development department, and then use these sub-departments to join the lower departments.

The results of implementation are as follows:

If you want to query all the superior departments of the development department as follows, just modify the recursive query statement above:

The implementation results are as follows:

The recursive common table expression' temp' cannot contain both.

Recursively query aggregate or non-windowed functions in a block

relational database

Mysql has restrictions on the depth of recursion, and the default recursion depth is 1000.

You can display a variable similar to "cte _ max _ recurrence _ depth"; Careful discussion

You can also display recursion and the variable Li Le' max _ execution _ time '; through the maximum execution time of the select statement;