如何在不使用联合的情况下从同一表的两列中获取数据?

时间:2019-01-12 11:55:26

标签: php select mysqli tree union

我有一些代码,其中我通过使用Union来获取同一表的数据,但现在我想不使用Union

    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 <> ''

结果显示为

    SId     TId
AMSNUDUCU   2
AMSGUWUDE   3
AMSXAWUXU   4
AMSZUCIVO   5
AMSFASOJA   6
AMSTEMUJI   7
AMSBIZEZO   8
AMSXUDINE   9
AMSZEVUTA   10
AMSREXEGI   11
AMSBIXIBO   12
AMSHEFOVI   13

以此类推

我有一个像这样的桌子:

选择*来自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   

,依此类推。 这个查询意味着一个父母有左孩子和右孩子,我通过联盟找到了单亲的所有子节点,但是我想用另一种方式来做,我的联盟查询有没有其他选择。因此,请专家帮助我,并以您的专业风格修改我的查询。

0 个答案:

没有答案