简体   繁体   English

PHP的MySQL得到所有父母

[英]php mysql get all parents

i'm trying to get all parents of children. 我正在努力让所有孩子的父母。 I have a simple query select from sections where sectionID = 6 The database structure is very simple 我有一个简单的查询,从sectionID = 6的部分中选择。数据库结构非常简单

sectionID parent
1     0
2     1
3     1
4     2
5     4
6     5

Now I'm trying to get all parents from sectionID 6, the result should be a string 0/2/4/5/6. 现在,我尝试从sectionID 6获取所有父母,结果应该是字符串0/2/4/5/6。 children might not have a lot of parents, it might have only one so the result should be different. 孩子可能没有很多父母,可能只有一个父母,所以结果应该有所不同。 ie 5/6.. I hope you understand what I'm trying to do. 即5/6 ..我希望您理解我正在尝试做的事情。 I have tried couple recursive functions which i've found on the internet, but I really suck at this and was wondering if anyone could help me to get on the right track. 我已经尝试过在互联网上找到的几个递归函数,但是我确实对此感到很困惑,并且想知道是否有人可以帮助我走上正确的道路。 PS I'm using php and mysql PS我正在使用PHP和MySQL

Well, as far as I can see you have two choices, both well known. 好吧,据我所知,您有两个选择,两个都很有名。

1) You make a recursive function, just the ones you have been trying. 1)您创建了一个递归函数,就是您一直在尝试的函数。 There are tons of them around and i won't put him in here. 他们周围有很多吨,我不会把他放在这里。

2) By far my favourite, this is the Database pattern most modern ORM use, it's called nested set model. 2)到目前为止,我最喜欢的是最现代的ORM使用的数据库模式,称为嵌套集模型。

Basically you create a few more columns on the table, it should look like this one: 基本上,您在表上又创建了几列,它看起来应该像这样:

CREATE TABLE nested_category (
        category_id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(20) NOT NULL,
        lft INT NOT NULL,
        rgt INT NOT NULL
);

INSERT INTO nested_category VALUES(1,'ELECTRONICS',1,20),(2,'TELEVISIONS',2,9),(3,'TUBE',3,4),
 (4,'LCD',5,6),(5,'PLASMA',7,8),(6,'PORTABLE ELECTRONICS',10,19),(7,'MP3 PLAYERS',11,14),(8,'FLASH',12,13),
 (9,'CD PLAYERS',15,16),(10,'2 WAY RADIOS',17,18);

SELECT * FROM nested_category ORDER BY category_id;

+-------------+----------------------+-----+-----+
| category_id | name                 | lft | rgt |
+-------------+----------------------+-----+-----+
|           1 | ELECTRONICS          |   1 |  20 |
|           2 | TELEVISIONS          |   2 |   9 |
|           3 | TUBE                 |   3 |   4 |
|           4 | LCD                  |   5 |   6 |
|           5 | PLASMA               |   7 |   8 |
|           6 | PORTABLE ELECTRONICS |  10 |  19 |
|           7 | MP3 PLAYERS          |  11 |  14 |
|           8 | FLASH                |  12 |  13 |
|           9 | CD PLAYERS           |  15 |  16 |
|          10 | 2 WAY RADIOS         |  17 |  18 |
+-------------+----------------------+-----+-----+

If you notice there is no parent_id column. 如果您发现没有parent_id列。 To be able to search for it's childrens for, let's say row 5 the query will be like: 为了能够搜索其子项,假设查询的第5行类似于:

Select * from nested_category where left > 7 and left < 8 order by left asc, which will bring no results. Select * from nested_category where left > 7 and left < 8 order by left asc,这不会带来任何结果。

For row number 1 the result however will bring the entire tree. 对于第1行,结果将带入整棵树。

I have no php script for autocreating those columns on this computer, but there are plenty around. 我没有用于在此计算机上自动创建这些列的php脚本,但是周围有很多。 Im afraid they are also recursive. 恐怕它们也是递归的。

You my find lots of info around searching for "nested set model", like this or theorical exaplanations of the model like this one 你我发现周围很多搜索“嵌套集模型”的信息,像这样或模型的理论框架exaplanations 像这样的

AND THIS IS A WAY DUPLICATED QUESTION (i can not put it as duplicated) 这是一个重复的问题(我不能将其重复)

Some other answers: 其他一些答案:

You should reread the forum rules before posting, look for already asked questions. 在发布之前,您应该重新阅读论坛规则,查找已经问过的问题。

Hope it helps. 希望能帮助到你。

I'm not going to write all the code, but this idea should work if I understand your database shema correctly. 我不会编写所有代码,但是如果我正确地理解了您的数据库shema,那么这个想法就应该起作用。

section = 6
result = ""
while section != ""
    select parent where sectionID = section
    result += parent
    section = parent
return result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM