Mysql 用ids保存层次结构替换类别名称
我有这样的Mysql 用ids保存层次结构替换类别名称,mysql,Mysql,我有这样的数据表: category_name | subcategory_name | other data --------------------------------------------- fruits | apples | ... fruits | oragnes | ... id | name | parent_id ------------------------ 1 | fruits | 0 2
数据
表:
category_name | subcategory_name | other data
---------------------------------------------
fruits | apples | ...
fruits | oragnes | ...
id | name | parent_id
------------------------
1 | fruits | 0
2 | apples | 1
3 | oragnes| 1
category_id | subcategory_id | other data
---------------------------------------------
1 | 2 | ...
1 | 3 | ...
用ID替换类别和子类别名称并将其移动到其他表中的最佳方法是什么?得到这样的结果:
类别
表格:
category_name | subcategory_name | other data
---------------------------------------------
fruits | apples | ...
fruits | oragnes | ...
id | name | parent_id
------------------------
1 | fruits | 0
2 | apples | 1
3 | oragnes| 1
category_id | subcategory_id | other data
---------------------------------------------
1 | 2 | ...
1 | 3 | ...
数据
表格:
category_name | subcategory_name | other data
---------------------------------------------
fruits | apples | ...
fruits | oragnes | ...
id | name | parent_id
------------------------
1 | fruits | 0
2 | apples | 1
3 | oragnes| 1
category_id | subcategory_id | other data
---------------------------------------------
1 | 2 | ...
1 | 3 | ...
我可以使用一些select distinct和join查询手动完成所有操作,但有更好的方法吗?可能有更简单的方法;但是mysql不包含我想在这里使用的许多窗口函数,也不包含常用的表表达式,也不包含带有用户变量的视图。。。。所以我留下了一点混乱,但它似乎工作 我假设你当前的模型可能有N个级别。 我还假设在多个类别下不存在子类别 它的作用是