我有一个自引用表,我想在单个查询中添加父示例和子示例。有没有更好的方法来做到这一点,然后以类似于我下面的方式分解它?
+---------------------+
| example |
+---------+-----------+
| id | parent_id |
+---------+-----------+
| 1 | |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | |
| 6 | 5 |
| 7 | 5 |
+---------+-----------+
DECLARE example_id INT;
INSERT INTO `example` (parent_id) VALUE("");
SET example_id = LAST_INSERT_ID();
INSERT INTO `example` (parent_id) VALUE (example_id);
最佳答案
如果您的问题指的是树结构,您最好检查 this article 。如果您的 parent 只有 1 级 child ,那么您的方法可能是最简单的方法。
关于mysql - 自引用表,单个查询中的父/子插入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6058511/