2017-09-25 82 views
0

所以我的產品類似這樣的表:獲得MySQL中來自同一個錶鏈接行

+--------+-----------+----------+------+ 
| serial | container | location | type | 
+--------+-----------+----------+------+ 
| A  | C1  | L1  | 1 | 
| B  | C1  | L1  | 2 | 
| C  | C1  | L1  | 3 | 
| D  | C2  | L1  | 1 | 
| E  | C2  | L1  | 2 | 
| F  | C2  | L2  | 1 | 
| G  | C2  | L2  | 2 | 
+--------+-----------+----------+------+ 

對於給定的串行我想在相同的返回類型3的產品序列集裝箱和位置。

例如鑑於序列= A,返回序列= C

我猜在同一個表上的內部聯接,但我還沒有能夠構建一個合適的SQL呢。所有幫助感激地接受:-)

回答

1
select serial 
from your_table 
where type = 3 
and concat(container, location) = (select concat(container, location) from your_table where serial = 'A') 

或更好

select t1.serial 
from your_table t1 
join your_table t2 on t1.container = t2.container 
        and t1.location = t2.location 
where t1.type = 3 
and t2.serial = 'A' 
+0

@jurgen d:感謝您的迅速反應。這足以讓我到那裏。我不得不做下面的事情來獲得足夠的唯一性:'和concat(容器,位置)=(選擇concat(容器,位置)...' – simonl

+0

是的 - 我更喜歡這個,非常感謝。 – simonl