# ST_Touches for more than just one geometry

I'd like to select some neighbor polygons with ST_Touches. Therefore I found the following statement:

``````SELECT b.name
FROM layera as a
JOIN layera as b
ON ST_Touches((SELECT a.geom FROM layera as a WHERE a.id = 5124),b.geom)
GROUP BY b.name
``````

With this I get all the neighbor polygons of the polygon with the id '5124'. But now I want to call this function with a list of arguments. See the example statment below:

``````SELECT b.name
FROM layera as a
JOIN layera as b
ON ST_Touches((SELECT a.geom FROM layera as a WHERE a.id > 5000),b.geom)
GROUP BY b.name
``````

(SELECT a.geom FROM layera as a WHERE a.id > 5000 returns lets say 100 results)

I know that this won't work because ST_Touches works for only two arguments. But this should show what I'd like to do. How is it possible to insert all the results of a SELECT-statement into this ST_Touches function?

Thanks for any suggestions. =)

EDIT:

Ok, I just found out that I get the same result set with this statement:

``````SELECT layera.name
FROM layera
WHERE ST_Touches(layera.geom,(SELECT layera.geom FROM layera WHERE layera.nr = 5134))
``````

Anyway, the problem isn't solved. =(

If I understand you right, you have just complicated the syntax a little:

``````SELECT a.nr, b.nr FROM
layera AS a INNER JOIN layera AS b
ON ST_Touches(a.geom, b.geom)
WHEREa.nr!=b.nr AND b.nr < 5000;
``````

Then you will get a list of all combinations between the first 4999 numbers and the rest.

• Thank you so much. This solved my problem. I can actually remove the `WHERE a.nr!=b.nr` because it returns the same result set as with it. Jun 3 '13 at 14:36

This is how I got around it

``````select *
from polygon
where st_disjoint( ( select polygon from polygons where id=1), neighbourhoods.polygon) =0
``````

See more details here https://stackoverflow.com/questions/26241751/find-neighbouring-polygons-in-ruby