2

我有一个这样的 MySQL 表:

mysql> SELECT ID, Username, Parent, LeftNum, RightNum FROM usercopy;

+----+------------+----------+---------+----------+
| ID | Username   | Parent   | LeftNum | RightNum |
+----+------------+----------+---------+----------+
|  1 | root       |          |       1 |       92 |
|  2 | robson     | perta    |      62 |       89 |
|  3 | budi       | root     |       2 |       35 |
|  4 | bejo       | root     |      36 |       47 |
|  5 | ben        | puff     |      13 |       24 |
|  6 | boll       | root     |      48 |       91 |
|  7 | charlie    | robson   |      63 |       80 |
|  8 | charles    | robson   |      81 |       88 |
|  9 | doni       | budi     |       3 |       28 |
| 10 | dono       | budi     |      29 |       34 |
| 11 | donatello  | bejo     |      37 |       38 |
| 12 | donat      | bejo     |      39 |       42 |
| 13 | tello      | bejo     |      43 |       46 |
| 14 | pras       | ben      |      14 |       15 |
| 15 | prasetyo   | ben      |      16 |       21 |
| 16 | pram       | ben      |      22 |       23 |
| 17 | jeni       | boll     |      49 |       58 |
| 18 | cocala     | boll     |      59 |       60 |
| 19 | perta      | boll     |      61 |       90 |
| 20 | joko       | charlie  |      64 |       65 |
| 21 | aditya     | charlie  |      66 |       79 |
| 22 | mogot      | aditya   |      67 |       70 |
| 23 | arya       | aditya   |      71 |       78 |
| 24 | saiful     | mogot    |      68 |       69 |
| 25 | nur        | arya     |      72 |       77 |
| 26 | herlambang | nur      |      73 |       74 |
| 27 | barney     | nur      |      75 |       76 |
| 28 | conan      | charles  |      82 |       85 |
| 29 | john       | charles  |      86 |       87 |
| 30 | mark       | conan    |      83 |       84 |
| 31 | mike       | doni     |       4 |       11 |
| 32 | puff       | doni     |      12 |       25 |
| 33 | karti      | doni     |      26 |       27 |
| 34 | sukarto    | mike     |       5 |        6 |
| 35 | harto      | mike     |       7 |        8 |
| 36 | tien       | mike     |       9 |       10 |
| 37 | tono       | dono     |      30 |       31 |
| 38 | thomas     | dono     |      32 |       33 |
| 39 | dunkin     | donat    |      40 |       41 |
| 40 | jentol     | tello    |      44 |       45 |
| 41 | henri      | prasetyo |      17 |       18 |
| 42 | yani       | prasetyo |      19 |       20 |
| 43 | juli       | jeni     |      50 |       51 |
| 44 | rio        | jeni     |      52 |       53 |
| 45 | luke       | jeni     |      54 |       55 |
| 46 | harry      | jeni     |      56 |       57 |
+----+------------+----------+---------+----------+

一组 46 行(0.00 秒)

我现在正在做的是根据他/她的父母使用 PHP LOOP 向小组成员展示,我非常讨厌它。但是,我不知道如何加入/组合这个 MySQL 表,以便通过单个 SQL 命令创建输出。

这是我使用 PHP 所做的,例如我的用户名是 'robson' :

1. `SHOW any users with parent name = robson` 

mysql> SELECT ID, Username, Parent, LeftNum, RightNum FROM usercopy WHERE Parent = 'robson';

+----+----------+--------+---------+----------+
| ID | Username | Parent | LeftNum | RightNum |
+----+----------+--------+---------+----------+
|  7 | charlie  | robson |      63 |       80 |
|  8 | charles  | robson |      81 |       88 |
+----+----------+--------+---------+----------+

2 行(0.00 秒)

 2. `SHOW Charlie's descendent` 

mysql> select ID, Username, Parent, LeftNum, RightNum from usercopy where LeftNum BETWEEN 63 AND 80;

+----+------------+---------+---------+----------+
| ID | Username   | Parent  | LeftNum | RightNum |
+----+------------+---------+---------+----------+
|  7 | charlie    | robson  |      63 |       80 |
| 20 | joko       | charlie |      64 |       65 |
| 21 | aditya     | charlie |      66 |       79 |
| 22 | mogot      | aditya  |      67 |       70 |
| 23 | arya       | aditya  |      71 |       78 |
| 24 | saiful     | mogot   |      68 |       69 |
| 25 | nur        | arya    |      72 |       77 |
| 26 | herlambang | nur     |      73 |       74 |
| 27 | barney     | nur     |      75 |       76 |
+----+------------+---------+---------+----------+

一组 9 行(0.00 秒)

3. LOOP Step 2 to SHOW Charles's descendent

mysql> select ID, Username, Parent, LeftNum, RightNum from usercopy where LeftNum BETWEEN 81 AND 88;

+----+----------+---------+---------+----------+
| ID | Username | Parent  | LeftNum | RightNum |
+----+----------+---------+---------+----------+
|  8 | charles  | robson  |      81 |       88 |
| 28 | conan    | charles |      82 |       85 |
| 29 | john     | charles |      86 |       87 |
| 30 | mark     | conan   |      83 |       84 |
+----+----------+---------+---------+----------+

4 行一组(0.00 秒)

现在的问题是如何使用单个 SQL 命令执行 STEP 2、STEP 3、...、STEP X(基于步骤 1 的多少后代)?

4

1 回答 1

1

我认为你需要一个递归查询,mysql 不支持。解决方法是使用不同的结构来存储层次结构或模拟您在 php 中所做的事情的存储过程。

Bill Karwin 在他关于MySQL 反模式的演讲中谈到了这种结构,这对于进一步研究来说非常有趣。

于 2012-04-16T11:17:49.417 回答