我正在尝试创建我认为是自我联接的东西,但是我无法获得它返回我想要的东西。我得到一个空集或一个关于非唯一表的错误
我试图简单地查询并仅返回“ 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 |
+----------+----------+-----------+----------+
答案 0 :(得分:0)
这就是我的工作方式
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;````