我正在尝试创建我认为是自连接的东西,但是我不能让它返回我想要的东西。我要么得到一个空集,要么得到一个关于非唯一表的错误
我试图简化查询,只返回'To Tag‘和'From Tag’,但我总是得到一个非唯一的表错误
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;我尝试加入的两个表是:终端关联:
+---------------+----------+----------------+----------+
| 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 |
+---------------+----------+----------------+----------+和终止计划
| 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 |
+----------+----------+-----------+----------+发布于 2019-05-03 17:08:05
下面是我是如何让它工作的
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;````https://stackoverflow.com/questions/55950716
复制相似问题