从mysql中的相同表中获取链接的行

时间:2017-09-25 10:37:46

标签: php 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的产品序列。

e.g。给定serial = A,返回serial = C

我猜测同一个桌面上的内部联接,但我还没能构建合适的SQL。感谢所有的帮助: - )

1 个答案:

答案 0 :(得分: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'
相关问题