Mysql:从多级分类中获取文章?
假设我有这样一个文章类别树:Mysql:从多级分类中获取文章?,mysql,recursive-query,multi-level,Mysql,Recursive Query,Multi Level,假设我有这样一个文章类别树: GrandPa Cate |______ Parent Cate 1 (article 1, article2) |______ Parent Cate 2 |______|_____ Child Cate 1 (article 3, article 4) |______|_____ Child Cate 2 (article 5) 表: 类别 id | parent_id | title ----------------------------------
GrandPa Cate
|______ Parent Cate 1 (article 1, article2)
|______ Parent Cate 2
|______|_____ Child Cate 1 (article 3, article 4)
|______|_____ Child Cate 2 (article 5)
表:
类别
id | parent_id | title
----------------------------------------
1 | null | GrandPa Cate
----------------------------------------
2 | 1 | Parent Cate 1
----------------------------------------
3 | 1 | Parent Cate 2
----------------------------------------
4 | 3 | Child Cate 1
----------------------------------------
5 | 3 | Child Cate 2
----------------------------------------
文章
id | cate_id | title
----------------------------------------
1 | 2 | article 1
----------------------------------------
2 | 2 | article 2
----------------------------------------
3 | 4 | article 3
----------------------------------------
4 | 4 | article 4
----------------------------------------
5 | 5 | article 5
----------------------------------------
选择Granpa Cate时,我如何才能获得全部5篇文章?你不容易做到。MySQL不支持递归查询,尽管如果为该查询编写例程,您可能能够管理递归调用查询。可以使用嵌套集模型,但这需要对表结构进行相当大的更改。如果级别数量有限,您可以编写连接代码来完成此操作(即,使用一系列左连接到树的最大深度)。另请参见和@NấmLùn你的最终解决方案是什么?