gpt4 book ai didi

mysql - 如何在mysql中连接表而不重复列

转载 作者:行者123 更新时间:2023-11-29 07:29:42 26 4
gpt4 key购买 nike

我有两个表:

表1(限制5):

+--------------+
| TF_ID |
+--------------+
| T139297_1.02 |
| T025433_1.02 |
| T025429_1.02 |
| T045327_1.02 |
| T088966_1.02 |
+--------------+

表2(限制5):

+--------------+-----------+------------+-------------+
| TF_ID | Family_ID | TSource_ID | DBID |
| T000001_1.02 | F001_1.02 | TS19_1.02 | FBgn0085089 |
| T000002_1.02 | F002_1.02 | TS07_1.02 | AMAG_03406 |
| T000003_1.02 | F002_1.02 | TS07_1.02 | AMAG_05299 |
| T000004_1.02 | F002_1.02 | TS07_1.02 | AMAG_10904 |
| T000005_1.02 | F002_1.02 | TS07_1.02 | AMAG_15258 |
+--------------+-----------+------------+-------------+

我想通过 TF_ID 列连接两个表,我已经尝试过:

select a.TF_ID, b.TF_ID, b.Family_ID from table1 a, table2 b where a.TF_ID=b.TF_ID; 

输出如下:

+--------------+--------------+-----------+
| TF_ID | TF_ID | Family_ID |
+--------------+--------------+-----------+
| T014411_1.02 | T014411_1.02 | F070_1.02 |
| T014431_1.02 | T014431_1.02 | F070_1.02 |
| T014439_1.02 | T014439_1.02 | F070_1.02 |
| T014447_1.02 | T014447_1.02 | F070_1.02 |
| T014450_1.02 | T014450_1.02 | F070_1.02 |

如果我不知道TF_ID列要重复,我该怎么办?我是数据库新手,请帮忙

最佳答案

只需选择一次;您不必选择两次。示例:

SELECT a.TF_ID, b.Family_ID
FROM table1 a, table2 b
WHERE a.TF_ID=b.TF_ID;

更好的是,使用USING:

SELECT a.TF_ID, b.Family_ID
FROM table1 a
LEFT JOIN table2 b USING (TF_ID);

关于mysql - 如何在mysql中连接表而不重复列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33117348/

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