我有2张这样的表:
表1
+----+-----------+--------------------+ | ID | Postcode4 | 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 | +----+------+-------+
我首先要选择beichgum中的regio,在这种情况下是regio 2.然后我希望表2中的所有其他4PP具有相同的regio(2)。所以查询的结果必须是9044,9045
答案 0 :(得分:3)
这是一个加入的人:)
SELECT t2.*
FROM Table1
INNER JOIN Table2 ON Table2.4PP = Table1.Postcode4
INNER JOIN Table2 t2 ON t2.Regio = Table2.regio
WHERE Table1.woonplaats = 'Beetgum'
答案 1 :(得分:1)
这是一个非常糟糕的查询,直到有人给你一个花哨的加入一个(你应该使用它)
select t.4PP from Table2 t
WHERE t.Regio in
(
select Regio
from Table2 t2
join Table1 t1 on t2.4PP=t1.Postcode4
where t1.woonplats='Beetgum'
)
答案 2 :(得分:0)
这应该这样做
select 4PP from table2 where Regio in (select Regio from table2 where 4PP in (select PostCode4 from table1 where woonplaats = "Beetgum"))
答案 3 :(得分:0)
似乎这就是你要找的东西:
SELECT GROUP_CONCAT(Table3.4PP) 4PP FROM Table1
INNER JOIN Table2 ON(Table1.PostCode4 = Table2.4PP)
INNER JOIN Table2 AS Table3 ON(Table2.Regio = Table3.Regio)
WHERE woonplaats = 'Beetgum';