首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >引用另一个表的MySQL自连接

引用另一个表的MySQL自连接
EN

Stack Overflow用户
提问于 2019-05-02 18:45:26
回答 1查看 26关注 0票数 0

我正在尝试创建我认为是自连接的东西,但是我不能让它返回我想要的东西。我要么得到一个空集,要么得到一个关于非唯一表的错误

我试图简化查询,只返回'To Tag‘和'From Tag’,但我总是得到一个非唯一的表错误

代码语言:javascript
复制
    select a.from_tag as 'From Tag', b.to_tag as 'To Tag'  
    from termination_schedule a
    inner join termination_schedule b 
    on terminal_associations.term_assoc_id=b.from_id
    inner join termination_schedule a 
    on terminal_associations.term_assoc_id=a.from_id;

我尝试加入的两个表是:终端关联:

代码语言:javascript
复制
    +---------------+----------+----------------+----------+
    | term_assoc_id | tag      | terminal_strip | terminal |
    +---------------+----------+----------------+----------+
    |             1 | BC122EE1 | XP             | L1       |
    |             2 | BC122EE1 | XP             | L2       |
    |             3 | BC122EE1 | XP             | L3       |
    |             4 | BC122HS1 | 1              | L1-1     |
    |            10 | BC122HS1 | 1              | L1-2     |
    |             5 | BC122HS1 | 1              | L2-1     |
    |            11 | BC122HS1 | 1              | L2-2     |
    |             6 | BC122HS1 | 1              | L3-1     |
    |            12 | BC122HS1 | 1              | L3-2     |
    |             7 | BC122M1  | 1              | L1       |
    |             8 | BC122M1  | 1              | L2       |
    |             9 | BC122M1  | 1              | L3       |
    +---------------+----------+----------------+----------+

和终止计划

代码语言:javascript
复制
| cable_no | from_id | to_id | core_mark |
+----------+---------+-------+-----------+
| 1001     |       1 |     4 | red       |
| 1001     |       2 |     5 | white     |
| 1001     |       3 |     6 | blue      |
| 1002     |      10 |     7 | red       |
| 1002     |      11 |     8 | white     |
| 1002     |      12 |     9 | blue      |
+----------+---------+-------+-----------+````

What I want to return is:
+----------+----------+-----------+----------+
| cable_no | From Tag | core_mark | To Tag   |
+----------+----------+-----------+----------+
| 1001     | BC122EE1 | red       | BC122HS1 |
| 1001     | BC122EE1 | white     | BC122HS1 |
| 1001     | BC122EE1 | blue      | BC122HS1 |
| 1002     | BC122HS1 | red       | BC122M1 |
| 1002     | BC122HS1 | white     | BC122M1 |
| 1002     | BC122HS1 | blue      | BC122M1 |
+----------+----------+-----------+----------+
EN

回答 1

Stack Overflow用户

发布于 2019-05-03 17:08:05

下面是我是如何让它工作的

代码语言:javascript
复制
select termination_schedule.cable_no, a.term_assoc_id as 'From Tag', termination_schedule.core_mark,b.term_assoc_id as 'To Tag'
FROM termination_schedule
LEFT JOIN terminal_associations a ON termination_schedule.from_id=a.term_assoc_id
LEFT JOIN terminal_associations b ON termination_schedule.to_id=b.term_assoc_id;````
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55950716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档