我有两张桌子:

table1(限制5):

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


table2(限制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/

10-12 15:47