我正在尝试使用self join
将此查询转换为一个查询。
SELECT leftn AS SId,
TId
FROM test_tree
INNER JOIN
(SELECT @sid:=
(SELECT group_concat(leftn, ',', rightn)
FROM test_tree
WHERE find_in_set(sid, @nsid)) , @nsid:=@sid AS fset
FROM test_tree,
(SELECT @nsid:='AMSJUTATE',@sid='')v)gr ON find_in_set(leftn, fset)
WHERE leftn <> ''
UNION (
SELECT rightn AS SId,
TId
FROM test_tree
INNER JOIN
(SELECT @sid:=
(SELECT group_concat(leftn, ',', rightn)
FROM test_tree
WHERE find_in_set(sid, @nsid)) , @nsid:=@sid AS fset
FROM test_tree,
(SELECT @nsid:='AMSJUTATE',@sid='')v)gr ON find_in_set(rightn, fset)
WHERE rightn <> ''
此查询表示父级有左子级和右子级,而我通过Union查找单个父级的所有子节点,但我想用另一种方式。有人可以转换此查询自我加入吗?我不知道如何转换此查询。
以下是我正在处理的数据的示例:
SELECT * FROM `test_tree`
TId SId LeftN RightN LStatus RStatus
1 - AMSJUTATE 1
2 AMSJUTATE AMSNUDUCU AMSFEZAVU 1 1
3 AMSNUDUCU AMSGUWUDE AMSTUTUNE 1 1
4 AMSFEZAVU AMSXAWUXU AMSHALURA 1 1
5 AMSGUWUDE AMSZUCIVO AMSKEFOFA 1 1
6 AMSTUTUNE AMSFASOJA AMSZAPUPE 1 1
7 AMSXAWUXU AMSTEMUJI AMSPOROBE 1 1
8 AMSHALURA AMSBIZEZO AMSFOHOFI 1 1
9 AMSZUCIVO AMSXUDINE 1
10 AMSKEFOFA AMSZEVUTA 1
11 AMSFASOJA AMSREXEGI 1
12 AMSZAPUPE AMSBIXIBO 1
13 AMSTEMUJI AMSHEFOVI 1
14 AMSPOROBE AMSLUPAKE 1
15 AMSBIZEZO AMSCUSIPU 1
16 AMSFOHOFI AMSCEMUSU 1
17 AMSXUDINE AMSKATOTU 1
18 AMSZEVUTA AMSLECEVU 1
19 AMSREXEGI AMSKOVUSA 1
20 AMSBIXIBO AMSLIWULO 1
21 AMSHEFOVI AMSJOLOWE 1
22 AMSLUPAKE AMSREROXE 1
23 AMSCUSIPU AMSPOMUBE 1
24 AMSCEMUSU AMSPULINU 1
25 AMSPULINU AMSWOFIVO 1