mysql RECURSIVE
时间: 2023-10-17 16:37:33 浏览: 134
MySQL does not natively support recursive queries like some other databases such as PostgreSQL or SQL Server. However, you can achieve a similar effect using common table expressions (CTEs) and self-joins.
Here is an example of using a CTE to simulate recursive behavior in MySQL:
```sql
WITH RECURSIVE cte AS (
SELECT * FROM your_table WHERE parent_id IS NULL
UNION ALL
SELECT t.* FROM your_table t
INNER JOIN cte ON t.parent_id = cte.id
)
SELECT * FROM cte;
```
In this example, `your_table` is the table you want to recursively query. The `parent_id` column represents the relationship between parent and child rows. The initial query selects the root nodes (rows with `parent_id` being NULL), and then recursively joins with the CTE until all related rows are retrieved.
Please note that the above code is just an example, and you need to modify it based on your specific table structure and requirements.
阅读全文