I have two tables:
User(id)
Region(id, userID, origin Geometry('point'), radius))
User has a one-to-one relationship with Region.
I want to query all users that have a region that contains a point (lat and long). To define contains, if the distance between the given point and region.origin is less than region.radius then that region contains the point.
I'm able to query just regions that contain a given point like this:
SELECT "id", "origin", "radius", "regionID", ST_Distance("origin", ST_MakePoint(lat, long), false) AS "distance"
FROM "regions" AS "region"
WHERE
ST_DWithin("origin", ST_MakePoint(lat, long), maxDistance, false) = true
AND
ST_Distance("origin", ST_MakePoint(lat, long), false) - radius <= 0
ORDER BY distance ASC
LIMIT 10;
But I really want the users and regions and don't want to query each user individually based of the region. Here is what I have:
SELECT *
FROM user, regions as region
WHERE
user.id = region.userID
AND
ST_DWithin("origin", ST_MakePoint(lat, long), maxDistance, false) = true
AND
ST_Distance("origin", ST_MakePoint(lat, long), false) - radius <= 0
LIMIT 10;
With this query, I don't get any results, where the first query I get results.
JOIN? – KKendall Nov 07 '18 at 04:57