Mysql 选择行中具有相同值的行
我有两张这样的桌子: 表1Mysql 选择行中具有相同值的行,mysql,Mysql,我有两张这样的桌子: 表1 ID - 4PP - woonplaats 1 - 9044 - Beetgum 2 - 9045 - Beetgummermole 3 - 1028 - Amsterdam 4 - 1029 - Amsterdam 5 - 1030 - Amsterdam 表2 ID - 4PP - Regio 1 - 9044 - 2 2 - 9045 - 2 3 - 1028 - 4
ID - 4PP - woonplaats
1 - 9044 - Beetgum
2 - 9045 - Beetgummermole
3 - 1028 - Amsterdam
4 - 1029 - Amsterdam
5 - 1030 - Amsterdam
表2
ID - 4PP - Regio
1 - 9044 - 2
2 - 9045 - 2
3 - 1028 - 4
4 - 1029 - 4
5 - 1030 - 4
我想从表2中选择所有的4PP,其中表1中的“woonplaats”是Beetgum,这些表可以通过4PP连接。因此,查询的结果必须是:90449045
select t2b.Regio from table1 t1
join table2 t2a on t1.4pp = t2a.4pp
join table2 t2b on t2a.Regio = t2b.Regio
where t1.woonplaats = "Beetgum";
这似乎是一个简单的连接,您尝试了什么?您已经提到了一个
join
,您尝试了什么?为什么输出应该包括9045?它的woonplates是Beetgummermole,而不是Beetgum。请看下面答案中的注释。如果我们不在了,我的意思是我想选择所有区域2的4PP(因为Woonplats在该区域)。但我只有Woonpaats供选择。。。
select 4PP
from table1 inner join table2 using (4PP)
where table1.woonplaats like '%Beetgum%'