gpt4 book ai didi

mysql - 查询自引用联结表

转载 作者:行者123 更新时间:2023-11-29 06:25:39 25 4
gpt4 key购买 nike

我的主表(表 1)包含字段 UIname

-----------------
UI name
-----------------
T001 Organism
T002 Plant
T004 Fungus
T005 Virus
T007 Bacterium
-----------------

第二个表(表 2)包含 name1name2 字段

---------------------
name1 name2
---------------------
Organism Organism
Organism Fungus
Plant Virus
Virus Bacterium
Organism Bacterium
---------------------

我需要生成以下输出

------------
UI1 UI2
------------
T001 T001
T001 T004
T002 T005
T005 T007
T001 T007
------------

这是我的初步尝试

SELECT * FROM table2 AS t2
JOIN table1 AS t1 ON t2.name1 = t1.name
JOIN table1 AS t3 ON t2.name2 = t3.name;

不幸的是,它返回了错误的行数。我想知道如何正确加入。

最佳答案

一种可能的方法是在 table1 上使用两个别名:

select
t1a.UI as UI1,
t1b.UI as UI2
from
table2 t2,
table1 t1a,
table1 t1b
where
t2.name1 = t1a.name and
t2.name2 = t1b.name;

关于mysql - 查询自引用联结表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31154384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com